Asynchronous programming in C#

Pritom Purkayasta
4 min readJul 23, 2021

What is asynchronously programming and how is it differ from synchronous programming?

I am picking this definition from wiki

Asynchrony, in computer programming, refers to the occurrence of events independent of the main program flow and ways to deal with such events.

In easy words, asynchronous means not having to wait for one task to start another task. How does it differ from synchronous programming then?

In Synchronous programming, code will execute block by block. by in asynchronous programming, one block will not wait for another to finish, it can run simultaneously not at the same time.

There are some misconceptions between asynchronous and parallelism as if you don’t understand properly, you will end up mixing them all.

The easy way to differentiate between asynchronous and parallel programming is, in asynchronous programming, you will need only one core to achieve these behaviors but in parallel programming, you will need dedicated core as much as possible. We will discuss this at another time.

In our real-life we use synchronous and asynchronous to finish our day, won’t we?

let’s understand this by a real-life example. I am going to describe a day of a software engineer (general daily routine) in perspective of synchronous and asynchronous programming.

Let’s just write down what a software engineer’s daily life looks like.

Wake up ⇒ Making Food ⇒ Having Breakfast ⇒ Office ⇒ Having Lunch ⇒ Self Study (optional) ⇒ Dinner ⇒ Sleep.

This is a general generic routine of any office worker in the world 😆.

Let’s draw this into synchronous programming concepts.

As in synchronous programming, code will be executed block by block. So if we run this it will run block by block.

Okay now let’s draw this routine in a fully asynchronous way. The diagram should look like this.

As mentioned before in asynchronous programming, a block of code (function) will not wait for another function to be finished. it can run simultaneously. So every function (steps) of our routine can run simultaneously. For better understanding, we will randomly time each function.

The whole code will finish executing after 9 Hours. Why?

In Asynchronous programming, all the tasks will queue in the thread pool (In c#). When we run our program, the main thread assigned tasks to the thread pool to complete. If we run synchronously, the main thread will block the entire process for one task to finish before starting another task. But in the asynchronous model, the main thread will start the first task (wake up), and see it is taking some time to finish so it will not wait and start the second task (making breakfast) and move on to the next tasks. So when the first task is finished before the second task in our case it will return the first task result. In that way, we are finishing the whole process in 9 hours’ time and the last task will be attending the office*.

Let’s visualize the concept with a diagram.

In the real-life, Do we do everything synchronously? The answer is no. So how do we execute our daily routine model in the context of real life? We use both synchronous and asynchronous models to complete our routine. Let’s look at this.

The Grouped blocked executing asynchronously and the single block executing synchronously.

I hope, the concept is a little bit clearer than before. :)

Want to see the implementation?

Goto GitHub and select async_101 branch 🎉

Originally published at https://pritompurkayasta.me.

--

--