Understanding Async/Await in C#

Muhammet Kaya
3 min readNov 18, 2019

--

Parallel Universe

Hello everybody,

This is my first story and I would like to share brief story about .NET Async/Await. I hope next stories come soon :)

If you are reading this story, you are already familiar concurrency, asyncronous programming or whatever you want to call it. I am not going to tell what is asynchronous/parallel programming. You know, asynchronous programming is an inevitable part of our work. When the things need to process in different thread such as calling an API we use background workers.

Background workers??

Background workers..
Without background workers, front side is?

Yes, they are back side and in our heart.❤

Anyway, to do something as a parallel, we need to use thread or task. They are our background workers. We use them carefully otherwise it leads to unexpected results like Race condition. But sometimes we code some blocks without aware of asynchronous programming. Some of developer do like that, especially beginners on asynchronous programming.

Short reminder, What is the difference between Task and Thread?

To execute some code asynchronously we need threads and they are part of our operation system. C# Threading library manipulate operations system’s thread library regards to it’s need.

Task is a library that is provided by Microsoft. Tasks use threadpool to do some jobs asynchronously. Threadpools reasons for being is limitation the usage of thread. Tasks use available thread to process it’s job. Also Tasks can return a value, thread can’t.

Async/Await keywords has appeared on C# version 5 at first. Async is use for indicate the method that can work asynchronously. Await is use for waiting the thread until process is done. To use await keyword you had to mark method async before.

Following example is a basic demonstration of async/await usage.

Async/Await Usage

Await is a magic keyword. I’m not saying, Microsoft says.

The await keyword is where the magic happens. It yields control to the caller of the method that performed await, and it ultimately allows a UI to be responsive or a service to be elastic.

Why “Await” is magical?

When I’ve faced with it, I thought async keyword like Task.Wait(). But It’s completely different. They work similar. This sitiuation cause to confuse us. We already mentioned about differences between Task and Thread. Task consume thread from Threadpool. Threadpool’s main goal is limit the thread usage.

For instance, a request come through your api.

When we use Task.Wait() for an async method to make sure it’s done. It will block the threadpool’s thread.

Task.Wait() Example

Image 25 more request come through your api and threadpool limit is 25. What happens? One more thing if the code call another async method. And then deadlock..

This scenario can happen easily. To prevent blocking the threadpool we should use await. I really recommend these keywords especially enterprise projects. Blocking threadpool can cause to problems or errors that you can’t identified. On the other hands, If you think Task.Wait() has used before, you should consider using ConfigureAwait().

That’s all I got to say. I hope you had enjoy of this story.

See you in next stories :)

Resources

  1. https://medium.com/rubrikkgroup/understanding-async-avoiding-deadlocks-e41f8f2c6f5d
  2. https://www.c-sharpcorner.com/article/task-and-thread-in-c-sharp/

--

--

Muhammet Kaya

Senior Software Developer @DrDoctor, Formerly @Setur, @LogoBusinessSolution