Using the await operator for the execution of parallel asynchronous tasks execution
In this recipe, we will learn how to use await
to run asynchronous operations in parallel instead of the usual sequential execution.
Getting ready
To step through this recipe, you will need Visual Studio 2012. There are no other prerequisites. The source code for this recipe can be found at BookSamples\Chapter5\Recipe4
.
How to do it...
To understand the use of await
operator for parallel asynchronous tasks execution, perform the following steps:
Start Visual Studio 2012. Create a new C# Console Application project.
In the
Program.cs
file, add the followingusing
directives:using System; using System.Threading; using System.Threading.Tasks;
Add the following code below the
Main
method:async static Task AsynchronousProcessing() { Task<string> t1 = GetInfoAsync("Task 1", 3); Task<string> t2 = GetInfoAsync("Task 2", 5); string[] results = await Task.WhenAll(t1, t2); foreach (string result in results...