Working with Task objects
Working directly with Task
objects can be extremely useful when introducing threading to existing projects. As we saw in the previous section, it is important to update the entire call stack when introducing async
and await
. On a large code base, those changes could be extensive and would require quite a bit of regression testing.
You can instead use Task
and Task<TResult>
to wrap the existing methods that you want to run asynchronously. Both Task
types represent the asynchronous work being done by a method or action. You use Task
when a method would have otherwise returned void. Use Task<TResult>
with methods that have a non-void return type.
Here are examples of two synchronous method signatures and their async equivalents:
public interface IAsyncExamples
{
void ProcessOrders(List<Order> orders);
Task ProcessOrdersAsync(List<Order> orders);
...