Author - StudySection Post Views - 297 views
programming

Asynchronous programming using Async Await in C#

In this topic, I will discuss an important concept of C# which can make your application more responsive and fast with the help of asynchronous programming.

Summary:-

In C# We can perform asynchronous programming by using async and await keywords. Asynchronous coding is required where you need to multi-thread your code. You need to start another task thread that can run independently of your main task thread. In the async approach, we can just execute long-running tasks and continue with other tasks.

These long-running tasks do the job in a different thread and when they complete it, they notify our main code, and our code can execute the next actions from here. Here we are referring to the main thread which deals with the UI or the thread that primarily processes a web request.

Benefits of Asynchronous programming.

  1. It improves application performance.
  2. It increases application responsiveness.
  3. Enable parallel programming
  4. You can perform other tasks while your time-consuming task is executing independently.
  5. It prevents the blocking of other independent tasks.
  6. It makes the UI more responsive.
  7. Make your application asynchronous just with these two keywords async-await.

Let’s understand how we can achieve asynchronous programming using async-await with some code examples.

Example 1:-

Let’s suppose we have software that manages the cooking process. We need to make breakfast which consists of tea and a sandwich. Let’s try it with synchronous code.
Programming class-code

Output:-

Programming class-code1
It has taken 15 seconds to make breakfast in the synchronous approach.
Now let see its result with an asynchronous approach using async-await.
class-code2

Output:-

class-code3
It has taken 10 seconds to make breakfast in an asynchronous approach.

Conclusion:-

In this example, we used the breakfast process to understand the importance of asynchronous programming. Our application needs to make tea and sandwiches. Let’s assume a sandwich takes 10 seconds to make and tea takes 5 seconds to make.
In the synchronous approach, it will first prepare a sandwich and then tea. We are waiting here for a sandwich and blocking the tea-making process which is completely independent of the sandwich-making process. It will wait for 10 seconds and then it will start the tea-making process. Therefore it will take 15 seconds to make breakfast. Here we have wasted 5 seconds by waiting for a sandwich for 10 seconds.
But in an asynchronous approach, we can run both tasks parallel to each other. Here we can make tea and sandwich at the same time, which will save us 5 seconds. Therefore it has taken only 10 seconds to make breakfast.
In big application scenarios where we need to deal with large amounts of data and big computations, asynchronous programming can make it more efficient and responsive.

Get certification for your knowledge in the fundamentals of Computer functioning by clearing the Computer Certification Exam conducted by StudySection. After going through this Computer Certification exam, you will be able to evaluate your basic knowledge of computers.

Leave a Reply

Your email address will not be published.