Posted on 4/4/2020 10:26:54 PM by Admin

Async Controller in MVC

In software world currently executing code block or program is called as a task, In Asp.Net framework every task is handled by a thread called as main thread until you do not assign a new thread to handle the task (multi threading).The IIS web server has a thread pool which actually allot threads to any task to execute it.

Everything in world is limited ,isn't it? so, how do you think thread pool will have unlimited threads. Thread pool too have limited threads .The situation, when all the threads are in use and for any new task ,there is no thread left in thread pool ,is called as Thread Starvation.

Async controller solves the thread starvation problem. Let's take an example of thread starvation and see how Async Controller solves it.

Suppose, we have to design a user dashboard where a user can check List of his friends, List of his posts till date and List of Questions he have ever asked like below diagram.
User Dashboard
User dashboard

Now ,let's understand how the above requirement can create thread starvation problem.

Suppose we have Dashboard view model and And ActionMethods Like below.

   public class DashboardViewModel
        public List Friends { get; set; }
        public List Questions { get; set; }
        public List Posts { get; set; }

         public ActionResult Dashboard()
            Task> t1 = Task.Factory.StartNew(() => GetFriends());
            Task> t2 = Task.Factory.StartNew(() => GetQuestions());
            Task> t3 = Task.Factory.StartNew(() => GetPosts());
            DashboardViewModel dvm = new Models.DashboardViewModel();
            dvm.Friends = t1.Result;
            dvm.Questions = t2.Result;
            dvm.Posts = t3.Result;
            return View(dvm);
        public  List GetFriends()
            List<friend> frs=new List<friend>();
            return frs;
        public List GetQuestions()
            List<question>ques = new List<question>();
            return ques;
        public List GetPosts()
            List<post> posts = new List<post>();
            return posts;

As soon as the Dashboard Action method is invoked a new thread called main thread is allotted to it by thread pool ,then for task t1, task t2 and task t3 three more threads are allotted to serve corresponding tasks , until all the tasks gets completed main thread hangs in air and does nothing, Threads are limited so any hanging thread is not good. In multi user scenario there may come a situation when count of hanging thread becomes too many and for new task no thread is left to serve it. Thus, it creates thread starvation.

The solution is to release the thread which is hanging and waiting for results to come.Async controller does the same ,it let the waiting thread free to serve new task.Now Let's implement Async Controller and check how it solve thread starvation problem.

To make a controller Async following are the 4 steps.
  1. Inherit your controller from AsyncController.
  2. Change your Action Method to return Task instead of ActionResult.
  3. Mark your Action Method with async keyword.
  4. Mark the method which waits for result with await keyword.

Async Controller
Async Controller

Now, as soon as the execution flow reaches to the method marked with the await keyword the main thread gets released, hence the thread starvation is solved.