Async and Await in Asynchronous Programming in C#

Introduction

Today, we'll learn about asynchronous programming. If we are talking about the asynchronous programming then Async and Await are the most important keywords used in it. We can reduce the barriers and improve the responsiveness of the application by using the asynchronous programming.

In the current .Net Framework (4.5.1) and Windows Runtime, asynchronous programming has the power of the asynchronous support. The compiler does the difficult work that the developer used to do and your application retains a logical structure that resembles synchronous code. By using all of this you can get the advantage of asynchronous programming.

Overview

Asynchronous programming is very essential when you are accessing a web resource and that web resource is already used by another process then it may be possible that, accessing that resource is slow or delayed and as a result the entire application must wait. The solution is the asynchronous programming, in it the application can continue with the rest work that does not depend on the resource.

Use of async and await

async

The async is the modifier to define the method as an asynchronous method. This keyword is the most important in the asynchronous programming. By the use of this you can create the asynchronous methods almost as simple as you create the synchronous method. The method with the use of async is indicated as an async method.

The following is the example to define an asynchronous method:

  1. public async Task<int> DisplayAsync()  
  2. {  
  3.     //statements;  
  4. }  

 

In the code above the DisplayAsync() method is an asynchronous method with the use of the async keyword. The async and await keywords are related to each other. If we define the method as an asynchronous method, it must have the await keyword otherwise the compiler will show an error.

await

The await operator is used in an asynchronous method to suspend the execution of the method until the awaited task completes. The method in which we use the await must be defined as an asynchronous method with the use of the async keyword.

the following is the example to define the await:

  1. public async Task<int> DisplayAsync()  
  2. {  
  3. var client = new HttpClient();  
  4. int length = (await client.GetStringAsync("www.c-sharpcorner.com")).Length;  
  5.        return length;  
  6. }  

 

In the code above, we have defined an asynchronous method as DisplayAsync() with the use of async and in the method we have used the await keyword and returned the integer value.

Async and Await

The Async and Await keywords are used in the Visual Basic and async and await are used in C#. If we specify a method as an async method:

  • The defined async method can use the Await or await to indicate the suspension points. The await operator stops the continuation until the awaited asynchronous process is complete.
     
  • The async method can itself be awaited by methods that call it.