I know how to make asynchronous methods but say I have a method that does a lot of work and then returns a boolean value?
How should I return the boolean value on the callback?
Clarification:
public bool Foo()
{
Thread.Sleep(100_000); // Do work.
return true;
}
I want to be able to make this asynchronous.
There are a few ways of doing that... the simplest is to have the async method also do the follow-on operation. Another popular approach is to pass in a callback, i.e.
void RunFooAsync(..., Action<bool> callback) {
// do some stuff
bool result = ...
if(callback != null) callback(result);
}
Another approach would be to raise an event (with the result in the event-args data) when the async operation is complete.
Also, if you are using the TPL, you can use ContinueWith
:
Task<bool> outerTask = ...;
outerTask.ContinueWith(task =>
{
bool result = task.Result;
// do something with that
});