Suppose you have a method that wraps an inner long-running method. This outer method may do a tiny amount of work before/after calling said long-running method. For example:
public async Task<int> LongRunningWrapperAsync()
{
int result = await LongRunningAsync();
result++;
return result;
}
It seems like the added weight of the boilerplate code generated by using async
is not necessarily worth the benefit of using await
, since its continuation is basically trivial. Therefore, given a sufficiently trivial* continuation, is it more performant to use Task.ContinueWith
? E.g.
public Task<int> LongRunningWrapperAsync()
{
return LongRunningAsync().ContinueWith(task => task.Result + 1,
TaskContinuationOptions.ExecuteSynchronously);
}
* Yes, both 'sufficiently' and 'trivial' are vague terms. Also, I've ignored exception handling in this contrived example. I suppose the need to handle exceptions implies that the continuation is non-trivial.