Web
Analytics
Synchronous and Asynchronous actions in ASP.NET Core | Angular | ASP.NET Tutorials

For Consultation : +91 9887575540

Stay Connected :

Synchronous and Asynchronous actions in ASP.NET Core

  • With asynchronous actions, as soon as a thread accepts an incoming request, it immediately passes it along to a background thread that will take care of it, releasing the main thread. This is very handy, because it will be available to accept other requests
  • This is not related to performance, but instead scalability; using asynchronous actions allows your application to always be responsive, even if it is still processing requests in the background.
  • First thing to do is adding async keyword to Action Method. If we use async Keyword in Method then the Method must also use await Keyword. The return type of an asyncmethod must be voidTask or Task<T> we have used Task<T> in Action Method
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;

using System.Threading.Tasks;
using System.Diagnostics;
using System.Linq;
using System.Threading;

namespace WebApplication33.Controllers
{
    public class asyncexController : Controller
    {
        public async Task<IActionResult> Index4()
        {

            List<emp> data = await getEmpList();
            return View(data);
        }
        public Task<List<emp>> getEmpList()
        {
            List<emp> empobj = new List<emp>
            {
                new emp{ id=1, name="Yogesh"},
                new emp{ id=2, name="Yogesh1"},
                new emp{ id=3, name="Yogesh2"}
            };

            return Task.FromResult(empobj);
        }
        public IActionResult Index6()
        {
            Stopwatch watch = new Stopwatch();
            watch.Start();
            ContentManagement service = new ContentManagement();
            var content = service.GetContent();
            var count = service.GetCount();
            var name = service.GetName();

            watch.Stop();
            ViewBag.WatchMilliseconds = watch.ElapsedMilliseconds;
            return View();
        }
        public async Task<ActionResult> Index5()
        {
            Stopwatch watch = new Stopwatch();
            watch.Start();
            ContentManagement service = new ContentManagement();
            var contentTask = service.GetContentAsync();
            var countTask = service.GetCountAsync();
            var nameTask = service.GetNameAsync();

            var content = await contentTask;
            var count = await countTask;
            var name = await nameTask;
            watch.Stop();
            
            ViewBag.WatchMilliseconds = watch.ElapsedMilliseconds;
            return View();
        }

    }
    public class emp
    {
        public int id { get; set; }
        public string name { get; set; }
    }

    public class ContentManagement
    {
        public string GetContent()
        {
            Thread.Sleep(2000);
            return "content";
        }

        public int GetCount()
        {
            Thread.Sleep(5000);
            return 4;
        }

        public string GetName()
        {
            Thread.Sleep(3000);
            return "Matthew";
        }
        public async Task<string> GetContentAsync()
        {
            await Task.Delay(2000);
            return "content";
        }

        public async Task<int> GetCountAsync()
        {
            await Task.Delay(5000);
            return 4;
        }

        public async Task<string> GetNameAsync()
        {
            await Task.Delay(3000);
            return "Matthew";
        }
    }

}