Edit (after acceptance)
It might not be immediately apparent but @Servy's answer is correct without needing a custom implementation of Unwrap
under monodroid - in my comments I said it didn't exist, but it definitely does.
End edit
I'm writing a bunch of apps that use our RESTful web services and, despite thinking I know how to use tasks properly it turns out I don't. The scenario is that I have code implemented for Windows Store - using async/await
and I need to implement something near-identical for MonoDroid - which doesn't have that (without some build hacks that I don't want to use).
I've boiled down the problem for this question to a simple set of tasks for getting an integer asynchronously, then, in the continuation, firing another asynchronous task that turns a string built from that integer. In C#5 this would be:
Note - of course I'm using Task.FromResult<T>
here in place of actual async code
private async Task<string> GetStringAsync()
{
return await GetStringFromIntAsync(await GetIntAsync());
}
private async Task<int> GetIntAsync()
{
return await Task.FromResult(10);
}
private async Task<string> GetStringFromIntAsync(int value)
{
return await Task.FromResult(value.ToString());
}
To convert this to a continuation-based pattern I tried this:
private Task<string> GetStringAsync()
{
//error on this line
return GetIntAsync().ContinueWith(t => GetStringFromIntAsync(t.Result));
}
private Task<int> GetIntAsync()
{
return Task.FromResult(10);
}
private Task<string> GetStringFromIntAsync(int value)
{
return Task.FromResult(value.ToString());
}
However, this isn't correct because the GetStringFromIntAsync
method returns a Task<string>
meaning that the continuation ends up returning a Task<Task<string>>
instead of a Task<string>
.
I've found that explicitly waiting on the GetStringFromIntAsync
method does work, however:
private Task<string> GetStringAsync()
{
return GetIntAsync().ContinueWith(t =>
{
var nested = GetStringFromIntAsync(t.Result);
nested.Wait();
return nested.Result;
});
}
The question is, though - is that the right way to do it - can I not return a continuation of some sort that the caller then waits on?
I have used tasks quite a bit - but not to chain tasks of different types together like this (except with async
/await
of course) - and feel a bit like I'm going mad here - so any help is greatly appreciated.
If you don't await the task or explicitly check for exceptions, the exception is lost. If you await the task, its exception is rethrown. As a best practice, you should always await the call. By default, this message is a warning.
async, await, and TaskThe await keyword waits for the async method until it returns a value. So the main application thread stops there until it receives a return value. The Task class represents an asynchronous operation and Task<TResult> generic class represents an operation that can return a value.
Run is misused to run IO blocking tasks. Although the code will work just fine (e.g UI not not freeze) but it is still a wrong approach. This is because Task. Run will still block a thread from thread pool the entire time until it finishes the method.
If you forget to use await while calling an async function, the function starts executing. This means that await is not required for executing the function. The async function will return a promise, which you can use later.
So, first off, your non-async/await imlementations of the second and third implementations is what you should do even when you have async/await. Using it adds nothing except a bit of unneeded overhead.
As for the first case, no, you don't want to use Wait
. That will block the thread instead of allowing it to be returned to the pool. You simply need to unwrap the task and get it's inner task. You can use the Unwrap
method to do that:
private Task<string> GetStringAsync()
{
return GetIntAsync()
.ContinueWith(t => GetStringFromIntAsync(t.Result))
.Unwrap();
}
Here is an Unwrap
function that you can use if one is not available to you.
public static Task<T> Unwrap<T>(this Task<Task<T>> task)
{
var tcs = new TaskCompletionSource<T>();
task.ContinueWith(t =>
{
t.Result.ContinueWith(innerT => tcs.SetResult(innerT.Result)
, TaskContinuationOptions.OnlyOnRanToCompletion);
t.Result.ContinueWith(innerT => tcs.SetCanceled()
, TaskContinuationOptions.OnlyOnCanceled);
t.Result.ContinueWith(innerT => tcs.SetException(innerT.Exception.InnerExceptions)
, TaskContinuationOptions.OnlyOnRanToCompletion);
}
, TaskContinuationOptions.OnlyOnRanToCompletion);
task.ContinueWith(t => tcs.SetCanceled()
, TaskContinuationOptions.OnlyOnCanceled);
task.ContinueWith(t => tcs.SetException(t.Exception.InnerExceptions)
, TaskContinuationOptions.OnlyOnFaulted);
return tcs.Task;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With