e.preventDefault(); // Prevent form submission input.blur(); // remove focus input.value = ""; // optional: clear the input

Async and Await in C#

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.

Related Posts

Latest Posts

  • All Posts
  • Generative AI
  • manufacturing
  • News
  • Portfolio
    •   Back
    • Android
    • iOS
    • Java
    • PHP
    • MEAN
    • Ruby
    • DotNet
    • IoT
    • Cloud
    • Testing
    • Roku
    • CMS
    • Python

India

Plot No. 11/2, Phase 3, Hinjewadi Rajiv Gandhi Infotech Park, Pune, India – 411057.
info@tudip.com
+91-96-8990-0537

United States

1999 S. Bascom Ave Suite 700, Campbell CA. 95008, USA.
info@tudip.com
+1-408-216-8162

Canada

64 Caracas Road North York, Toronto Ontario M2K 1B1, Canada.
info@tudip.com

Mexico

Calle Amado Nervo #785 Interior B Colonia Ladron De Guevara 44600 Guadalajara, Jalisco, Mexico.
info@tudip.com

Colombia

Cra. 9 # 113-53 Of. 1405 Bogotá D.C., Colombia.
info@tudip.com

UAE

Tudip Information Technologies L.L.C Office No 109, ABU HAIL BUILDING 13, Abu Hail, Dubai, UAE.
info@tudip.com

Nigeria

22 Kumasi Crescent, Wuse 2, Abuja, Nigeria.
info@tudip.com