Asynchronous Programming In C#

Part 1 - An Introduction

Subscribe to my newsletter and never miss my upcoming articles

Both synchronous and asynchronous programming methods are supported by C#.

What you will learn in Part 1 of this series:

  1. An analogy in simple English to understand what Asynchronous means.
  2. What is synchronous?
  3. What is the solution to the synchronous problem?

1. Asynchronous (An analogy)

The analogy that we are going to study to understand asynchronous pattern is (How to make breakfast)

  • Pour a cup of coffee.
  • Heat up a pan, then fry two eggs.
  • Toast two pieces of bread.
  • Add butter and jam to the toast.
  • Pour a glass of orange juice.

If you have experience with cooking, you'd execute those instructions asynchronously. You'd start warming the pan for eggs, you'd put the bread in the toaster, then start the eggs. At each step of the process, you'd start a task, then turn your attention to tasks that are ready for your attention.

Cooking breakfast is a good example of asynchronous work that isn't parallel. One person (or thread) can handle all these tasks. Continuing the breakfast analogy, one person can make breakfast asynchronously by starting the next task before the first one completes. The cooking progresses whether or not someone is watching it. As soon as you start warming the pan for the eggs, you can begin toasting the bread. Once the bread starts toasting, you can pour the juice into the glass.

For a parallel algorithm, you'd need multiple cooks (or threads). One would make the eggs, one the bread, and so on. Each one would be focused on just that one task. Each cook (or thread) would be blocked synchronously waiting for the egg to be ready to flip, or the toast to pop.

2. What is Synchronous?

  • When a set of activities starts executing one at a time, it is known as synchronous. (In other words, A synchronous call waits for the method to complete before continuing with program flow.)
  • Interestingly enough, any method we normally create in C# is synchronous by default.

Is synchronous bad?

  • Yes and No
  • Yes because the advantage of the synchronous code is that its step-by-step actions make it easy to scan and understand.
  • No because
    1. It badly impacts the UI that has just one thread to run its entire user interface code.
    2. Synchronous behavior leaves end users with a bad user experience and a blocked UI whenever the user attempts to perform some lengthy (time-consuming) operation.

3. The solution to the synchronous problem :

A synchronous method call can create a delay in program execution that causes a bad user experience. Hence, an asynchronous approach (threads) will be better. An asynchronous method call (creation of a thread) will return immediately so that the program can perform other operations while the called method completes its work in certain situations.

The asynchronous method's behavior is different than synchronous ones because an asynchronous method is a separate thread. You create the thread; the thread starts executing, but control is immediately returned back to the thread that called them at the time; while the other thread continues to execute.

In general, asynchronous programming makes sense in two cases,

  1. If you are creating a UI-intensive application in which the user experience is the prime concern.
  2. If you have other complex or expensive computational work to do, you can continue interacting with the application UI while waiting for the response back from the long-running task.

By Vikipediaaa , Website: Vikas Saraf

No Comments Yet