nikeee nikeee - 2 months ago 5
C# Question

How to correctly implement a TAP method?

I want to provide a task-based asynchronous pattern-style method. When awaiting the method, I could not find any difference between these two ways of providing the method:

// GetStats is a delegate for a void method in this example
public Task GetStatsAsync()
return TaskEx.Run(GetStats);

public async Task GetStatsAsync()
return await TaskEx.Run(GetStats);

// Usage:
await GetStatsAsync();
// Difference?

The upper method seems to have less overhead than the lower one. When looking at the MSDN blogs, I noticed that they seem to use the lower method. (For example in this article)

Why? What exactly is the difference? They both seem to work.


Those are both logically the same, but the second one has more overhead and is not recommended for that reason.

You may find my async intro helpful, as well as the task based asynchronous pattern document.

For more information on the overhead of async, I recommend the Zen of Async by Stephen Toub.

You probably also want to read "Should I Expose Asynchronous Wrappers for Synchronous Methods?" In short, the answer is "no."