In this article we will discuss about the two new keywords that were introduced in C# 5.0, for implementing asynchronous programming. So let’s start with some basic their definitions, rules we have 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 by using the syntax
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(), we call it using the code as:
function async void Method2()
Task: The task or function which is to be made asynchronous, has a return type of the type
Task which represents
void return type or
TResult is the type of data returned by the function. So, for our method above, i.e.
Method2, assuming that it returns an integer value, the signature will be of the following type:
function Task<Int32> Method2()
So this was the basic discussion. Now let’s convert the above code into an example. For this, our asynchronous method or
Method2, will be something which 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:
Next, we create another function inside which we will call this function using
await keyword. The reason we are not calling this function in the
Main function is that we need to make the parent function as
Main function cannot be made
async. So we have 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, which will help us to evaluate and understand the flow of the program in better way. So our complete code becomes like the following:
Run this code and see the results:
Let’s try to understand the flow of the program with the above 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, which in turns
Method2. As the
Method2 is marked with the
await keyword, it starts its execution in an
asyncrhonous mode, suspend 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 print 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
So this was about the use of the
await keywords. Hope you enjoyed reading it…!!!