In order to get a clear vision of these keywords, async and await, let us first understand the concept of synchronous and asynchronous programming.
It is a method of programming in which tasks are performed one at a time. The next task will be executed only after the completion of the previous task. In other words, we need to wait for the previous task to be complete to move to the next one.
Example: Suppose there are two methods – Method1 and Method2 and let us assume both methods are independent of each other and Method1 takes a longer time to complete its task. So, it will execute Method1 first and it will wait for the completion of this method, and then it will execute Method2. Thus, it will be a time-consuming process even though both methods are independent.
! By default, C# methods are synchronous.
Below is the coding example that fetches data from the database and binds it to the textbox synchronously.
Advantages of Synchronous Programming:
- Suppose if there is a timeout or a crash in the execution of task 1 and the system needs to recover from it, then it will recover from the exact point where it got crashed.
- It is beneficial for systems having low specifications as it requires less processing power.
- Provides code synchronization.
Disadvantages of Synchronous Programming:
- If there is a time-consuming operation on a button click on a UI like reading a large file, then the entire application has to wait to complete the whole task. In other words, if any operation is blocked in a synchronous application, the whole application gets blocked, and the application stops responding until the whole task completes.
- It leaves end users with a bad experience If UI has just one thread to run its entire user interface.
! And here comes the need of using Asynchronous programming that is a solution to the Synchronous problem.
Asynchronous Programming: It is a method of programming that allows more than one task to be executed at the same time. It means that we can execute multiple things at a time and we don’t have to wait for the current task to finish before moving on to the next one.
So does it means that asynchronous programming improves the performance of an application?
The answer is NO. It neither improves performance nor increases execution speed.
Now the question arises:
Why should we use it?
The answer is simple:
An asynchronous program can handle multiple requests at the same time with the same resources which improves throughput.
Asynchronous programming = Improved throughput.
Let us understand this through a real-world example:
Suppose you go to a restaurant. A waiter comes to your table to take the order and gives it to the kitchen. While the chef is preparing your food, the waiter goes to another table and takes the order, and delivers it to the kitchen. Here the waiter can serve many different tables without waiting for the chef to prepare the first order. This is called asynchronous architecture. It means that the asynchronous program results in better utilization of the available resources.
We make use of async and await keywords to implement asynchronous programming in C#. These are the code markers, which mark code positions from where the control should resume after a task completes.
Code example of C# async await:
In the above example, Method1 returns length as an integer value that is passed as a parameter in Method3. So, Method3 is dependent on the output of Method1. Here, we have to use await keyword before passing a parameter in Method3 and for it, we have to use async keyword from the calling method. A new method is created as callMethod which calls all methods as Method1, Method2, and Method3.
When should we use asynchronous programming?
Asynchronous programming should be used when there is a need to perform time-consuming I/O bound operations. It is a better fit for code that must respond to events – for example, any kind of graphical UI.
When should we use synchronous programming?
When the application does not have to perform I/O bound or other time-consuming operations that involve waiting time. And, when the application involves operations such as to render video or any mathematical calculations are completely dependent on the CPU.
Leave A Comment