Asynchronous programming in C-Sharp

Imagine a soft drinks manufacturing company that typically bottles the drink and sells it in the market. Even though the final product of this company is a soft drink, the process of manufacturing can be divided into two subprocesses. One where the bottles are getting manufactured and another where the drinks are getting manufactured. When both these products are ready, the drink is filled in the bottle, sealed, and packed.

If you notice here, the manufacturing of the bottles and drink were not interdependent. Both the manufacturing processes were happening in parallel in such a way that neither the drink nor the bottle had to wait for the completion of the other product.

Let us now look at an example in the software industry. There is a certificate validation website, which accepts as many as 10 certificates at a time. It provides the authenticity of these certificates by validating them against the corresponding issuing authority. Suppose in this workflow, the application takes one certificate at a time, validates it, and then picks the next certificate, the amount of time the user will be waiting for this to get completed will be very high. This, in turn, will reduce the incoming users to the website. But, imagine if the website can get the input of all the 10 certificates and validate it simultaneously so that the user doesn’t need to wait for a longer time. Will this be not an encouraging factor for the user to visit again and again to validate more and more things? The validation process is after all much quicker. That is the advantage of doing things asynchronously.

What is asynchronous programming?

Asynchronous programming is an effective way to reduce the delay or wait time that is happening in the code. It avoids the following scenario — an activity is blocked in a synchronous process, which will in turn block the entire application by blocking the other tasks from executing.

Keywords async and await are used in C# for achieving asynchronous programming. To make a function asynchronous, the async keyword will be added in front of the method name. Below is an example of the syntax of an asynchronous method.

public async Task DoWork(){ }

When to use asynchronous programming?

Whenever there is a blocking code that is independent of the remaining process in the code execution, then asynchronous programming can be implemented.

On executing a particular method asynchronously, it gets executed in a separate thread and not in the main thread. Also, for long-running tasks, asynchronous programming can be implemented to make the main thread free.

Asynchronous programming can be implemented in the below areas:

  1. File read/write operations that are independent of the main activity of the application.

  2. Third-party API calls

  3. Loading of maps or special plugins that needs to be loaded in the application other than the main content.

  4. Code logics that are independent of the main operation. Example — Logging