Asynchronous programming 101 - C#

Asynchronous programming 101 - C#

Understanding asynchronous programming in C#

ยท

3 min read

What is asynchronously 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. 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?

Example

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.

Planning

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 ๐Ÿ˜†.

Synchronous workflow:

Let's draw this into synchronous programming concepts.

image.png

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.

Asynchronous workflow:

Untitled.png

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

image.png

If we run this whole code synchronously it will finish after 33,037 minutes of computation time but if we ran this asynchronously it will finish after 9 Hours. Why?

Explanation:

In Asynchronous programming, all the tasks will queue in the thread pool (In c#). When we run our program, the main thread assigns 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.

image.png

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.

image.png

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

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

Code Implementation

Want to see the implementation?

Goto github and select async_101 branch ๐ŸŽ‰