Understanding async and await in .NET 4.5
The new asynchronous capabilities of .NET 4.5 rely on the async
and await
modifiers. Basically we have two important points here:
- The async modifier indicates to the compiler that a method or lambda expression is asynchronous—we call them async methods.
- The
await
operator, which can only be used within an async method, is applied to a task to suspend execution of the method until the task is complete. Meanwhile, the control is returned to the caller of that method.
How to do it...
Here we will use the
async
and await
features in a basic way to clearly understand them.
- Create a new Visual Studio project of type Console Application named
caAsyncAwait
. - Add a reference to the
System.Net.Http
assembly. - In the ...
Get Microsoft .NET Framework 4.5 Quickstart Cookbook now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.