How to make an Asynchronous Method return a value?

Overload119 picture Overload119 · May 18, 2011 · Viewed 124.8k times · Source

I know how to make Async methods but say I have a method that does a lot of work then returns a boolean value?

How do I return the boolean value on the callback?

Clarification:

public bool Foo(){
    Thread.Sleep(100000); // Do work
    return true;
}

I want to be able to make this asynchronous.

Answer

Dave Arkell picture Dave Arkell · May 18, 2011

From C# 5.0, you can specify the method as

public async Task<bool> doAsyncOperation()
{
    // do work
    return true;
}

bool result = await doAsyncOperation();