This is a series of articles about Multi-threading.
- Multi-Threading (1), Concept: What, Why
- Multi-Threading (2), Implementation Overview
- Multi-Threading (3), async, await in C# --- this article
- Multi-Threading (4), delegate based Asynchronous Programming Model
- Multi-Threading (5), Summary
Introduction
The first article in this series, Multi-Threading (1), Concept: What, Why, written on 01/30/2023, introduced the multi-threading concept. The following articles should be the implementations of different approaches.
I used to plan to start from the original approach, the delegate implementation, then go to Asynchronous Programming Model (APM), Event-based Asynchronous Pattern (EAP), and Asynchronous Programming Model (APM). However, due to being busy with something else, I did not have much time to think about this topic; I even lost the logical clue of this series of articles. Occasionally, I saw one piece of notes, an interview question about multi-threading, very interesting, that leads to this article.
The async and await keywords, although they have been used in C# for more than 10 years, this implementation is a very new approach, quite different from the traditional one. Probably, just due to this reason, I may start from here, introducing the async and await and the newest implementation approach, without the impression from the old approaches; that might be a good approach because if someone jumps into the programming world, the C# world, they might not know the traditional implementation approaches. Let us start from here.
I will use the interview question as an example to discuss the async and await keywords and the multi-threading program in C# in detail.
The content of the article,
- Introduction
- Problem
- "async" and "await" Key Words in C#
- Thread Flow Analyses
- Solution
- Synchronous Process
The next article, Multi-Threading (3-1), async, multi-await, will give more example analysis from different angles.
Problem
We start with the interview question: how to get the expected output:

By one of three workflows:
(1)

(2)

(3)

"async" and "await" Key Words in C#
async and await are two new keywords introduced into C# 5.0 in 2012. At that time, CPU clock speed reached an upper limit imposed by physical laws. But chip makers started to deliver CPUs with several cores that can run tasks in parallel. Thus C# needed a way to ease asynchronous programming.
The "async" keyword marks a method asynchronous, meaning it can be run in the background while another code executes. When you mark a method as async, you can use the "await" keyword to indicate that the method should wait for the result of an asynchronous operation before continuing.
Code sample
public async Task<int> ExampleMethodAsync()
{
var httpClient = new HttpClient();
int exampleInt = (await httpClient.GetStringAsync("http://msdn.microsoft.com")).Length;
ResultsTextBox.Text += "Preparing to finish ExampleMethodAsync.\n";
// After the following return statement, any method that's awaiting
// ExampleMethodAsync (in this case, StartButton_Click) can get the
// integer result.
return exampleInt;
}
async in C#
- An asynchronous method is defined by using the
asynckeyword and is referred to as an async method. - An async method is usually named with a suffix
Async,such as in the .NET Base Class Library (BCL),butand is not mandatory. This suffix can be ignored in methods with common names likeButton1_Click()orMain(). By the way,Main()is also marked withasyncin the code above; more on this point later.
await in C#
- In the async method, once the keyword
awaitis met for the first time- First, the remaining of task is actually executed by some random threads obtained from the runtime thread pool.
- Secondly, the calling method to the async method is not blocking the main thread; it will return to the first calling code that is not marked as await.
The async and await keywords are the heart of async programming. By using those two keywords, one can create an asynchronous method almost as easily as creating a synchronous method, even without really understanding the runtime workflow.
Note


















Join the conversation! Your thoughts help the community grow.