private async static Task GetResult()
{
HttpClient client = new HttpClient();
var task = client.GetAsync(“http://www.google.com”);
var str = await task; //start a new thread to access google.com
var task2 = task.ContinueWith(e =>
{ //start another new thread to resume the execution after http get
System.Threading.Thread.Sleep(10000);
if (e.IsCompleted)
{
Console.WriteLine(str.Content.ReadAsStringAsync().Result);
}
});
Console.WriteLine(“Waiting for the result”);
await task2; //The whole GetResult task consists of two sub-tasks(two steps before completion)
}
static void Main(string[] args)
{
GetResult().Wait(); //Block the main thread before exit, no problem, no deadlock.
Console.ReadKey();
}