"await" Expression and Child Thread

A tutorial example is provided on how to use the 'await' expression on an awaitable task to launch a child thread to wait for the task to complete.

After learning the asynchronous behavior of the GetStringAsync() method from the HttpClient class, we can take a closer look at the "await" operator now.

According to the C# 5 specification, the "await" operator works under the following rules:

The evaluation of "await t" goes roughly like this:

Based on Microsoft examples, the task returned by "GetStringAsync()" seems to be awaitable task. So let's use it in an "await" expression in a tutorial example, AwaitExpressionTest.cs:

// AwaitExpressionTest.cs
// Copyright (c) 2016 HerongYang.com. All Rights Reserved.

using System;
using System.Net.Http;
using System.Threading.Tasks;
public class AwaitExpressionTest {
   static void Main() {
      Console.WriteLine("Before 'await' ...");	
      AwaitTest();
      Console.WriteLine("After 'await' ...");	
   }
   async static void AwaitTest() {
      string uri = "https://www.herongyang.com/Service/Hello_REST.php";
      HttpClient c = new HttpClient();
      Console.WriteLine("Calling GetStringAsync()...");	
      Task<string> t = c.GetStringAsync(uri);
      Console.WriteLine("Evaluating await-expression ...");	
      string s = await t;
      Console.WriteLine("Writing task result ...");	
      Console.WriteLine(s);
   }
}

If you compile and run this tutorial example with .NET Framework 4.6.1 SDK, you will get:

C:\herong>%NET%\csc "/r:%REF%/System.Net.Http.dll"
   AwaitExpressionTest.cs
Microsoft (R) Visual C# Compiler version 4.6.1055.0
for C# 5
Copyright (C) Microsoft Corporation. All rights reserved.

C:\herong>AwaitExpressionTest
Before 'await' ...
Calling GetStringAsync()...
Evaluating await-expression ...
After 'await' ...

Not too bad. The tutorial example worked. But the remaining portion of the AwaitTest() method after the "await" expression did not get a chance to be executed. Here is my understanding of what happened:

Child thread #2 was supposed to continue with statements after "await t" after Child thread #1 reached to the end. But main thread ended before that occurred as shown in the diagram below:

Await Expression Child Thread
Await Expression Child Thread

Table of Contents

 About This Book

 Introduction of C# (C Sharp)

 Data Type and Variables

 Logical Expressions and Conditional Statements

 Arrays and Loop Statements

 Data Type Features

 Floating-Point Data Types

 Passing Parameters to Methods

 Execution Environment Class

 Visual C# 2010 Express Edition

 Class Features

 C# Compiler and Intermediate Language

 Compiling C# Source Code Files

 MSBuild - Microsoft Build Engine

 Memory Usages of Processes

 Multithreading in C#

Async Feature from C# 5

 What Is Async Feature?

 GetStringAsync() Method in HttpClient Class

 GetStringAsync() Method Example Program

 Watching Asynchronous Operation Execution Status

"await" Expression and Child Thread

 "await" Expression Thread Example

 "async" Function Example

 System.IO.FileInfo Class

 System.Diagnostics.FileVersionInfo Class

 WPF - Windows Presentation Foundation

 Partial Classes and Partial Methods

 Archived Tutorials

 References

 Full Version in PDF/ePUB