In the old days we used BackgroundWorkers, but these days we want to create tasks we can run with the async keyword.
Here is an example for a task that will return a boolean:
public async Task Test()
{
bool result = false;
Exception error = null;
await Task.Run(() =>
{
try
{
result = someSlowComponent.getBooleanResult();
}
catch (Exception ex)
{
error = ex;
}
});
if (error != null)
throw error;
else
return result;
}
You can now await this method and make it asynchronous:
public async bool GetValue()
{
bool result = await Test();
//Do more things with the result.
return result;
}
No comments:
Post a Comment