0

I asked a question today How can I run a method without waiting for long running process and I got a good answer and ajax solved my problem. But now my question is why should I still use async await keywords?

public async Task<ActionResult> DoSomeAsyncStuff()
{
    var model = new MyModel();
    await Task.Delay(20000);//My long-running process is getting data from an API with "HttpWebRequest".

    model.Name = "Something";
    //Assigning other model properties

    return PartialView("_InnerView", model);
}

Isn't the async await keywords to avoid blocking UI? But in the web I can't see any blocking UI in my case in the web unlike WinForms. The above code and ajax works great even without async await keywords. So someone please can explain why should I use them and what is their usages?

  • in a general way: `ajax` is `client-side` and `async/wait` is `server-side` way of work with async operation. Read a full explanation [here](https://stackoverflow.com/questions/26978034/difference-between-ajax-helper-and-async-await) – Elmer Dantas Aug 10 '17 at 08:43
  • @ChristianGollhardt My question is about their benefits in web not application. In web I can't see any blocking UI issue. So my question is different from duplicate one. –  Aug 10 '17 at 08:43
  • Where is the difference @SteveCode? Even Ajax can benefit from parallel processing. – Christian Gollhardt Aug 10 '17 at 08:44
  • You'll want to read up on the Task Parallel Library. This is probably too big of a concept to fit into a reasonable Stack Overflow answer. – Tieson T. Aug 10 '17 at 08:44

2 Answers2

0

If I'm not mistaken the async, in this case, let you handle more requests on the server: while you have the delay (waiting data from SQL? FS?) another request may be accepted and starts its cycle.

LoreX75
  • 58
  • 8
  • 1
    Actually when you await, that thread will be released and it will be send back to thread pool. So any other request can use that.. – Power Star Aug 10 '17 at 08:47
  • Thank you @PowerStar, that exactly what I would have like to say, probably I was not that clear :) – LoreX75 Aug 10 '17 at 08:51
  • @PowerStar when you await, that thread will be released and it will be send back to thread pool. So the awaited task runs where when thread is released? –  Aug 10 '17 at 09:34
  • @SteveCode Actually at that time your system will not take care about that. Once the awaited process is completed then that will send a callback flag to you. So at that point new thread will be created to continue with that process. – Power Star Aug 10 '17 at 09:37
  • @PowerStar Well if the system will not take care about that how is possible that awaited process will be completed. Shouldn't it runs on a thread or something like that at least? I want to know where it runs until it completes? –  Aug 10 '17 at 09:40
  • Async methods don't require multithreading because an async method doesn't run on its own thread. The method runs on the current synchronization context and uses time on the thread only when the method is active. – Power Star Aug 10 '17 at 09:53
  • For CPU-bound code, you await an operation which is started on a background thread with the Task.Run method. – Power Star Aug 10 '17 at 09:56
0

The reason requests are async is to allow for multiple requests to be handled by a single thread, rather than anything regarding a UI (A server doesn't really have a UI after all, it just returns files or data in some fashion). If the method was synchronous, the thread handling this request would be blocked while you wait, and if you're receiving a large number of requests at once, you can quickly rack up the number of threads you use. As threads do come with some overhead, this can negatively impact the performance of your server as a result.

By using async/await, this thread is freed to handle another request at the same time which is both much quicker and much more efficient than just blocking a thread for a while. In your example, one thread can handle multiple calls of DoSomeAsyncStuff() simultaneously - When it hits await Task.Delay(20000);, another request can then be handled either resuming from a previously hit await or from the start of the method.

  • this thread is freed to handle another request at the same time. So the awaited task runs where when thread is released? –  Aug 10 '17 at 09:35
  • When the awaited task completes, it will either resume on it's original thread or another thread from the thread pool if the original thread is being used by another request. – Daniel Masterson Aug 10 '17 at 09:40
  • Thanks but my question is When the awaited task completes in this process of completion it should use some thread for complete. Not? if the system will not take care about that how is possible that awaited process will be completed. Shouldn't it runs on a thread or something like that at least? I want to know where it runs until it completes? –  Aug 10 '17 at 09:42