Introduction

This sample contains the code for How to: Make Multiple Web Requests in Parallel. The sample shows you how to separate creating a task from awaiting the task if your program has other work to accomplish that doesn’t depend on the completion of the task.
Step by step instructions can be found in the How-to topic.

Building the Sample

  1. Unzip the downloaded file.
  2. Open Visual Studio 2012 or Visual Studio Express 2012 for Windows Desktop.
  3. On the menu bar, choose File, Open, Project/Solution.
  4. Navigate to the directory that holds the unzipped sample.
  5. Open the solution (.sln) file.
  6. Choose F5 or Ctrl+F5 to build and run the sample.
Description
Async makes it easy to set up asynchronous applications. In most cases, you await an asynchronous call immediately, but you don't have to. If the calling method has other work to do that is independent of the results of the asynchronous call, you can call the async method, do the independent work, then await the results of the call.
In the following example, a call is made to an async method, someWebAccessMethodAsync. The method returns a Task or Task<TResult>, which is assigned to myTask. At that point, myTask is active, but the calling method can continue to do other work until myTask is awaited.
 
C#Visual Basic
Edit|Remove
// The following line creates and starts the task. 
var myTask = someWebAccessMethodAsync(url); 
 
// While the task is running, you can do other work that doesn't depend 
// on the results of the task. 
// . . . . . 
 
// The application of await suspends the rest of this method until the task is complete. 
var result = await myTask;
This sample fires off three web downloads without immediately awaiting them. The downloads can run in parallel until the tasks are awaited.

More Information

For more information on async programming, see Programming with Async and Await.