Async and Await in C#

Async and Await in C#

10 March 2021

Introduction

If a programmer wants to run some part of code separately from the main thread execution, he/she will make use of Asynchronous Programming.

.NET Framework has given us two keywords, async and await to transform our code from synchronous to an Asynchronous way for it.

Benefits of asynchronous programming

We might have come across some applications which are sometimes unresponsive and take more time to load than usual. It happens because the application is trying to execute tasks behind other particular tasks, which takes a longer time than usual. Because of it, other tasks had to wait. This process is known as a synchronous way of doing things. Asynchronous programming does not wait for the other task hence increases the responsiveness of the application. Even though there will not be a major change in time taken to finish the task between synchronous and asynchronous , an asynchronous call thread is not blocked from responding to other requests.

Asynchronous VS Multithreading

Asynchronous is not multithreading because multithreading works on separate threads with no interaction between threads,while asynchronous threads leave and return at some point while in execution. To resume the execution at some point it has to be stored on something, and to store .NET uses a SynchronizationContext (It represents a location where code might be executed).

Structure of async and await

Async:

A keyword async is used to qualify a method as an asynchronous method. In other words, if we specify the async keyword in front of a method then we can call this method runs asynchronously. The syntax is:

public async void CallMethod()  

{  }

Await:

The await keyword is used when we want to call any function asynchronously. The syntax is:

await LongProcess();

Return type for an asynchronous method

The return type for an asynchronous function is Task. In other words we can say that, when it finishes its execution it will complete a Task. Each and every asynchronous method can return these three types of values.

  1. Void: Means nothing to return
  2. Task: It will perform one operation which is little similar to void.
  3. Task<T>: Will return a task with a T type parameter.

The following example shows how to declare and call a method that returns a Task<TResult> or a Task:

Task<TResult>

async Task<int> GetTaskOfTResult()

{

    int hours = 0;

    await Task.Delay(0);

    return hours;

}

Task<int> returnedTaskTResult = GetTaskOfTResult();

int intResult = await returnedTaskTResult;

Task

async Task GetTask()

{

    await Task.Delay(0);

}

Task returnedTask = GetTask();

await returnedTask;

await GetTask();

Conclusion

The asynchronous keywords should be used when it does not involve user Interface. Asynchronous programming increases the responsiveness of the application. Asynchronous deals with application throughput, which means the performance of tasks is by the computing service but not by application performance because throughput is a way of measuring performance. The improper use of async void should be avoided as the compiler will not warn us about it. The Task-based asynchronous pattern should be used for any asynchronous programming implementation.

search

Blog Categories

Request a quote