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.
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.
- public ActionResult GetList() {
-
- var watch = new Stopwatch();
- watch.Start();
- var country = GetCountry();
- var state = GetState();
- var city = GetCity();
- watch.Stop();
- ViewBag.WatchMilliseconds = watch.ElapsedMilliseconds;
- return View();
- }
The above method code is in "synchronize programming". Stopwatch is used for getting exact execution time.
Add GetListAsync() ActionResult in Home Controller.
- public async Task < ActionResult > GetListAsync() {
-
- 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.
Add the below methods which are used in
GetList() and
GetListAsync().
- #region-- > GetCountry Methods
- for GetList && GetListAsync
- public string GetCountry() {
- Thread.Sleep(3000);
- return "India";
- }
- public async Task < string > GetCountryAsync() {
- await Task.Delay(3000);
- return "India";
- }#endregion
- # region-- > GetState Methods
- for GetList && GetListAsync
- public string GetState() {
- Thread.Sleep(5000);
- return "Gujarat";
- }
- public async Task < string > GetStateAsync() {
- await Task.Delay(5000);
- return "Gujarat";
- }#endregion
- # region-- > GetCity Methods
- for GetList && GetListAsync
- public string GetCity() {
- Thread.Sleep(6000);
- return "Junagadh";
- }
- public async Task < string > GetCityAsync() {
- await Task.Delay(6000);
- return "Junagadh";
- }#endregion
After executing synchronize method, we see that it takes 14002 milliseconds.
After executing asynchronize method, we saw that it took 6012 ms.