locked
Async and Await Blocking Ui RRS feed

  • Question

  • User577498514 posted

    I am new to ASP.Net Mvc , i have gone through some async and await video and articles,so i use it in my action like

    public IActionResult Index()
    {
        var jsonTask  = await GetJsonAsync(new Uri("https://jsonplaceholder.typicode.com/comments"));
    LogEvent("Index","Async operation 1 started"); var jsonTask2 = await GetJsonAsync(new Uri("https://jsonplaceholder.typicode.com/comments")); LogEvent("Index","Operation 2 started");

    ViewBag.Result1= jsonTask; ViewBag.Resulr2 = jsonTask2;
    return View(); }

    Issue is when i debug the application it execute in sequential order and jsonTask1 then LogEvent(...) and so on, why my LogEvent is not fire when doing async job of JsonTask1 and why control not goes to View if it is async processing of Json Tasks.

    Tuesday, October 29, 2019 11:18 AM

Answers

  • User-1780421697 posted

    When you use await key word with a function then it start waiting for the result and not going further, to do some operations in parallel you need to initiate the functions and then wait for their completion after sync or other method that need to be executed in parallel.

    like

        /// <returns></returns>
            public async Task<IActionResult> Index()
            {
                //Fire
                var t1 =  GetAsync(); //starting        
                var t2 =  GetAsync(); //starting 
                var t3 =  GetAsync(); //starting
                
                AnySyncMethod();      //processing
                Debug.WriteLine("Other operation");   //done
                Debug.WriteLine("Good going");        //done
                
    
                //May be at this time t1 or t2 or t3 already done beause of parallel execution 
                var r1 = await t1;   //waiting for result
                var r2 = await t2;   //waiting for result
                var r3 = await t3;   //waiting for result
    
                    
                return View();
            }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 30, 2019 3:32 AM

All replies

  • User753101303 posted

    Hi,

    This is mainly for web forms but https://docs.microsoft.com/en-us/aspnet/web-forms/overview/performance-and-caching/using-asynchronous-methods-in-aspnet-45#Parallel applies as well.

    The xxxAsync method starts the operation and await allows to wait for this operation to be completed. This is the usual pattern when you have a single async operation or needs to make them one after the other.

    The code sample shows how to call just the xxxAsync methods to start them all and then you'll await on all of them so that  the next line happens when all are ended...

    Tuesday, October 29, 2019 11:54 AM
  • User-1780421697 posted

    When you use await key word with a function then it start waiting for the result and not going further, to do some operations in parallel you need to initiate the functions and then wait for their completion after sync or other method that need to be executed in parallel.

    like

        /// <returns></returns>
            public async Task<IActionResult> Index()
            {
                //Fire
                var t1 =  GetAsync(); //starting        
                var t2 =  GetAsync(); //starting 
                var t3 =  GetAsync(); //starting
                
                AnySyncMethod();      //processing
                Debug.WriteLine("Other operation");   //done
                Debug.WriteLine("Good going");        //done
                
    
                //May be at this time t1 or t2 or t3 already done beause of parallel execution 
                var r1 = await t1;   //waiting for result
                var r2 = await t2;   //waiting for result
                var r3 = await t3;   //waiting for result
    
                    
                return View();
            }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 30, 2019 3:32 AM