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 to 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.
![MVC project](https://www.csharp.com/admin/async-await-and-asynchronous-programming-in-mvc10032020095549/Images/01.jpg)
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.
![ASP.NET Select a template](https://www.csharp.com/admin/async-await-and-asynchronous-programming-in-mvc10032020095549/Images/02.jpg)
![Change Authentication](https://www.csharp.com/admin/async-await-and-asynchronous-programming-in-mvc10032020095549/Images/03.jpg)
STEP 02 Add synchronize and asynchronize methods.
Add GetList() ActionResult in Home Controller.
- public ActionResult GetList() {
- //Create a stopwatch for getting excution time
- var watch = new Stopwatch();
- watch.Start();
- var country = GetCountry();
- var state = GetState();
- var city = GetCity();
- watch.Stop();
- ViewBag.WatchMilliseconds = watch.ElapsedMilliseconds;
- return View();
- }
Add GetListAsync() ActionResult in Home Controller.
- public async Task < ActionResult > GetListAsync() {
- //Create a stopwatch for getting excution time
- var watch = new Stopwatch();
- watch.Start();
- var country = GetCountryAsync();
- var state = GetStateAsync();
- var city = GetCityAsync();
- var content = await country;
- var count = await state;
- var name = await city;
- watch.Stop();
- ViewBag.WatchMilliseconds = watch.ElapsedMilliseconds;
- return View();
- }
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.
- #region-- > GetCountry Methods
- for GetList && GetListAsync
- public string GetCountry() {
- Thread.Sleep(3000); //Use - when you want to block the current thread.
- return "India";
- }
- public async Task < string > GetCountryAsync() {
- await Task.Delay(3000); //Use - when you want a logical delay without blocking the current thread.
- return "India";
- }#endregion
- # region-- > GetState Methods
- for GetList && GetListAsync
- public string GetState() {
- Thread.Sleep(5000); //Use - when you want to block the current thread.
- return "Gujarat";
- }
- public async Task < string > GetStateAsync() {
- await Task.Delay(5000); //Use - when you want a logical delay without blocking the current thread.
- return "Gujarat";
- }#endregion
- # region-- > GetCity Methods
- for GetList && GetListAsync
- public string GetCity() {
- Thread.Sleep(6000); //Use - when you want to block the current thread.
- return "Junagadh";
- }
- public async Task < string > GetCityAsync() {
- await Task.Delay(6000); //Use - when you want a logical delay without blocking the current thread.
- return "Junagadh";
- }#endregion
After executing synchronize method, we see that it takes 14002 milliseconds.
![asynchronous programming](https://www.csharp.com/admin/async-await-and-asynchronous-programming-in-mvc10032020095549/Images/04.jpg)
After executing asynchronize method, we saw that it took 6012 ms.
![asynchronous programming](https://www.csharp.com/admin/async-await-and-asynchronous-programming-in-mvc10032020095549/Images/05.jpg)
Author
Ankit Kanojia
243
7.8k
1.8m