I have a class that awaits an asynchronous thread to return. What I do is launch a while loop and check every second where it's at.
public class AsychronousThingy
{
public bool IsComplete{get;private set;}
public string Result {get;private set;}
public void StartDoingThings(){
IsComplete = false;
// kick of an asynchronous thread to do stuff...
}
private void OnComplete(){
IsComplete = true;
Result = "magical result";
}
}
public class ActionExecutor{
public async Task<CustomThingyTaskResult> DoStuff(){
var thing = new AsychronousThingy();
thing.StartDoingThings();
// this returns instantly...?
while(!thing.IsComplete){
await Task.Delay(1000);
}
// do stuff once the 'thing' is complete.
return new CustomThingyTaskResult(thing.Result);
}
}
class Program{
public static void Main(){
var exec = new ActionExecutor();
var returnValue = await exec.DoStuff();
}
}
I'm unsure why it is returning instantly. Is this an issue - can you not use Task.Delay inside a loop? Odd.
I am sure that this is returning before it is complete as when I console.writeline from the OnComplete event of my async class it actually completes after my main method has finished.
DoStuffshould return aTaskawaitcan't be used inMain.