Async and Await in C#

So let's start with some basic their definitions with rules we need to follow to implement it.

Await: As per MSDN, await is:

The await operator is applied to a task in an asynchronous method to suspend the execution of the method until the awaited task completes. The task represents ongoing work.

This means, if we apply this keyword to a method and when the control reaches that method, that method will start running in asynchronous mode and the control will move on to the next statement in the program. This is simply done using the syntax: await Method_Name().

Async: Simply adding this keyword will not work. The method in which await is used, must be marked with the async keyword. So if we need to make a method (say Method1Async()) asynchronous using await, inside a method named Method2() then we call it using the code as in the following:

  1. function async void Method2()  
  2. {   
  3.    await Method1Async();  
  4. }  
Task: The task or function that is to be made asynchronous, has a return type of the type Task that represents a void return type or Task<TResult>, where TResult is the type of data returned by the function. So, for our method above, in other words Method2, assuming that it returns an integer value, the signature will be of the following type:
  1. function Task<Int32> Method2()  
  2. {  
  3.    return 1;  
  4. }  
So this was the basic discussion. Now let's convert the code above into an example. For this, our asynchronous method or Method2, will be something that takes time to complete and returns us an integer value. In our case, we will simply make it sleep using Thread.Sleep and then return an integer value. So it's signature will become like the following:

cs code

Next, we create another function inside that that we will call this function with using the await keyword. The reason we are not calling this function in the Main function is that we need to make the parent function as async and the Main function cannot be made async. So we need to introduce an intermediate function named Method1(). Next, we simply need to call this function in the Main function. We are also printing different values that will help us to evaluate and understand the flow of the program in a better way. So our complete code becomes like the following:

task

Run this code and see the results:

output

Let's try to understand the flow of the program with the preceding output: 
  1. The program execution starts with the execution of the Main function and prints the very first line "Starting the ASYNCHRONOUS process".

  2. Next, it moves on to call Method1, that in turn calls Method2. As the Method2 is marked with the await keyword, it starts its execution in an asyncrhonous mode, suspends the further execution of the Method1 and immediately returns to the calling statement in the Main function, without printing the message in the very next line.

  3. Then it continues the Main function and starts processing the for loop and prints the message.

  4. Finally when execution of the asynchronous method is completed, it again resumes the execution of the Method2 and prints the results, with the return value received from Method2.

So this was about the use of the async and await keywords. I hope you enjoyed reading it!

Recommended Ebook

Diving Into OOP

Download Now!
Similar Articles