Introduce Async/Await in C#

Energy boy
3 min readJun 23, 2022

--

In the last article, we introduce the asynchronous process by telling a making breakfast story. In this article, I will implement it using C#. Besides, we will deep dive into how async/await syntax really works in C#.

Before reading this article, please make sure you have finished my previous article.

Code Example of Making Breakfast

Below is the synchronous process of making breakfast…

It is intuitive to get the result of 8 mins.

Let’s do the asynchronous version.

As you can see, we only take 5 mins to finish our breakfast. BTW, please take a close look to see when do the task start and finish.

Code Explanation

As you saw, there are a lot of “await” in our asynchronous code. How can we interpret it in the easy way?

In my experience, await means two things to me when reading code…

First, When your program need an I/O bound task, such as calling API or downloading files, in these case “await” means it will not block the thread and waiting for the result. Instead, it will jump back and see whether there have another job to do.

Second, “Await” also means that the function guarantee to return you a result or finish the job before we keep running the code below await syntax.

And don’t worry about “async”, it is just a mark to tell the compiler we have an await syntax inside our function and it return type will be Task.

So how can we read a asynchronous code by moving your finger?

In the beginning, just move your finger in Main() from top to down until you reach makeCoffeeTask = MakeCoffeeAsync(). Move your finger to the top of MakeCoffeeAsync function and start moving down until you reach “await”. According to my first experience we will jump back and see another job, so we will execute fryEgg() and fryBacon(). These two function is not asynchronous so it will hold our resource until it finished. Let’s keep moving our finger to toastBreadTask= toastBreadAsync(). Similar with coffee task, we move our finger to the top of function toastBreadAsync() and start go down until we reach “await”, same as I told before, we will jump back to Main(). We keep moving to “await makeCoffeeTask”. If we apply my first experience, that will be strange right? There is nothing to jump until we get the result of coffee. It is better to consider this situation as second experience. It will guarantee to return you a result. But It will not block the thread while waiting for the result. Same explanation in “await toastBreadTask”. Needless to say, you already know what will happen when we move to Combine().

Conclusion

In C#, asynchronous function should run as a Task. It will be executed while the task is established. The programmer decide when we need the result of that asynchronous function. The most important thing is that we have to know when should I get the task result to keep running my code or your code will run like synchronous function. Keep learning and keep trying. You will get familiar with this syntax.

--

--

Energy boy
Energy boy

Written by Energy boy

Start my Software Engineer career on March 2019. I love to deep dive into complicated concept and share it.