Asynchronous Programming
So far, you have created tasks and used the static Task
factory methods to run and coordinate such tasks. In earlier versions of C#, these were the only ways to create tasks.
The C# language now provides the async
and await
keywords to mark a method as asynchronous. This is the preferred way to run asynchronous code. Using the async
/await
style results in less code and the code that is created is generally easier to grasp and therefore easier to maintain.
Note
You may often find that legacy concurrent-enabled applications were originally created using Task.Factory.StartNew
methods are subsequently updated to use the equivalent Task.Run
methods or are updated directly to the async
/await
style.
The async
keyword indicates that the method will return to the caller before it has had a chance to complete its operations, therefore the caller should wait for it to complete at some point in time.
Adding the async
keyword to a method instructs the...