awaiting inside an awaitable function in .net
Solution 1:
Most likely you just need (note the await
on the last line):
public async Task<int> DoIt()
{
await DoOtherAsyncStuff();
StartDoingStuff();
return await _tcs.Task;
}
await
is needed on the last line because an async
function returning Task<int>
needs to return int
, while _tcs.Task
is a Task<int>
. Using await
will wait for the Task's completion and return the int
which is what we need.
However, depending on your complete code you may want something else. For example if you're doing more complicated things with TaskCompletionSource
you may need to remove async
for this definition and do something like
public Task<int> DoIt()
{
return DoOtherAsyncStuff().ContinueWith(_ =>
{
StartDoingStuff();
return _tcs.Task;
}, TaskCompletionOptions.ExecuteSynchronously);
}
In general it's best not to mess with TaskCompletionSource unless you're doing something more advanced, for example providing an async abstraction of something synchronous/callback based. Hence a complete code example may change my answer (for example what's the body of StartDoingStuff
+ DoOtherAsyncStuff
?).