Async, Await And Asynchronous Programming In MVC

Introduction

Let’s first establish what the purpose of this code is, in the first place.

For this article, the purpose of the code is to understand what async / await and asynchronous programming is, how to use asynchronous programming, benefits of using asynchronous programming. In this article, I will create a simple MVC application to elaborate the answers of all the above questions.

Async

Async keyword is used to call the function/method as asynchronously.

Await

Await keyword is used when we need to get result of any function/method without blocking that function/method.

Asynchronous Programming 

Asynchronous Programming means parallel programming. By using Asynchronous Programming, the compiler can execute multiple functions / methods at same time without blocking any function / method.

We will create 3 methods here, and ensure that all these methods take specific time for execution.

STEP 01 Create new MVC Application project, named as "Async".

In the File menu, click New Project.


In the "New Project" dialog box, under Project types, expand Visual C#, and then click "Web". In the Name box, type "Async", then click on Ok.

Now, in the dialog box, click on "MVC" under the ASP.NET 4.5.2 Templates. Then, click on "Change Authentication" at the center of the right side.





STEP 02 Add synchronize and asynchronize methods.

Add GetList() ActionResult in Home Controller.
  1. public ActionResult GetList() {  
  2.     //Create a stopwatch for getting excution time  
  3.     var watch = new Stopwatch();  
  4.     watch.Start();  
  5.     var country = GetCountry();  
  6.     var state = GetState();  
  7.     var city = GetCity();  
  8.     watch.Stop();  
  9.     ViewBag.WatchMilliseconds = watch.ElapsedMilliseconds;  
  10.     return View();  
  11. }  
The above method code is in "synchronize programming". Stopwatch is used for getting exact execution time.

Add GetListAsync() ActionResult in Home Controller.
  1. public async Task < ActionResult > GetListAsync() {  
  2.     //Create a stopwatch for getting excution time  
  3.     var watch = new Stopwatch();  
  4.     watch.Start();  
  5.     var country = GetCountryAsync();  
  6.     var state = GetStateAsync();  
  7.     var city = GetCityAsync();  
  8.     var content = await country;  
  9.     var count = await state;  
  10.     var name = await city;  
  11.     watch.Stop();  
  12.     ViewBag.WatchMilliseconds = watch.ElapsedMilliseconds;  
  13.     return View();  
  14. }  
The above method code is in "asynchronize programming". Stopwatch is used for getting exact execution time. As we discussed earlier,
  • async is for calling the GetListAsync() method asynchronously.
  • await is used to execute GetCountryAsync(), GetStateAsync() and GetCityAsync()  parallelly without blocking any of them.
Add the below methods which are used in GetList() and GetListAsync().
  1. #region-- > GetCountry Methods  
  2. for GetList && GetListAsync  
  3. public string GetCountry() {  
  4.     Thread.Sleep(3000); //Use - when you want to block the current thread.  
  5.     return "India";  
  6. }  
  7. public async Task < string > GetCountryAsync() {  
  8.     await Task.Delay(3000); //Use - when you want a logical delay without blocking the current thread.  
  9.     return "India";  
  10. }#endregion  
  11. # region-- > GetState Methods  
  12. for GetList && GetListAsync  
  13. public string GetState() {  
  14.     Thread.Sleep(5000); //Use - when you want to block the current thread.  
  15.     return "Gujarat";  
  16. }  
  17. public async Task < string > GetStateAsync() {  
  18.     await Task.Delay(5000); //Use - when you want a logical delay without blocking the current thread.  
  19.     return "Gujarat";  
  20. }#endregion  
  21. # region-- > GetCity Methods  
  22. for GetList && GetListAsync  
  23. public string GetCity() {  
  24.     Thread.Sleep(6000); //Use - when you want to block the current thread.  
  25.     return "Junagadh";  
  26. }  
  27. public async Task < string > GetCityAsync() {  
  28.     await Task.Delay(6000); //Use - when you want a logical delay without blocking the current thread.  
  29.     return "Junagadh";  
  30. }#endregion  
After executing synchronize method, we see that it takes 14002 miliseconds.



After executing asynchronize method, we saw that it took 6012 ms.


Up Next
    Ebook Download
    View all
    Learn
    View all