"await" Expression Thread Example

A tutorial example is provided to confirm that the 'await' expression child thread will execute the remaining portion of the method if it has enough time to wait for the task to complete.

From the previous tutorial, we learned that when an "await" expression is evaluated, a child thread is launched to wait and finish the rest of the method. The current thread exits the method immediately. If the current thread ends the program too early, you may not have a chance to finish its job as shown in the previous tutorial.

To confirm this understanding, I modified the example code to ask the main thread to sleep for some time to see what will happen:

// AwaitExpressionWaiting.cs
// Copyright (c) 2015, HerongYang.com, All Rights Reserved.

using System;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
public class AwaitExpressionWaiting {
   static void Main() {
      Console.WriteLine("Before 'await' ...");	
      AwaitTest();
      Console.WriteLine("After 'await' ...");
      Console.WriteLine("Now I am waiting ...");
      Thread.Sleep(1000);
   } 
   async static void AwaitTest() {
      string uri = "http://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" 
   AwaitExpressionWaiting.cs

C:\herong>AwaitExpressionWaiting
Before 'await' ...
Calling GetStringAsync()...
Evaluating await-expression ...
After 'await' ...
Now I am waiting ...
Writing task result ...
<Result>
   <Message>
      Hello from server - herongyang.com.
   </Message>
</Result>

Perfect! The output matches my expectation that the "await" expression child thread will execute the remaining portion of the method if it has enough time to wait for the task to complete.

Last update: 2015.

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

 Outdated Tutorials

 References

 PDF Printing Version