Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC4 Async controller - Why to use?

I am trying to understand why and when should I use an async controller action. Eventually, when I use await in it, it will wait for the operation to complete in order to return the View.

For example

public async Task<ActionResult> TryMe() {    await SomeActionAsync();    return View(); } 

In this case if I use the async or not using the async, the Action will take the same time to execute.

If I am not trying to run at least 2 slow operations (that are not dependent on each other) in parallel, I don't see any reason to use an async controller action.

Please correct me if I'm wrong. I think I'm missing something here.

like image 234
DorR Avatar asked Mar 03 '13 19:03

DorR


People also ask

What is the primary purpose of using an async controller action Mcq?

async actions help best when the actions does some I\O operations to DB or some network bound calls where the thread that processes the request will be stalled before it gets answer from the DB or network bound call which you just invoked.

What is primary purpose of using an async controller action?

The asynchronous controller enables you to write asynchronous action methods. It allows you to perform long running operation(s) without making the running thread idle. It does not mean it will take lesser time to complete the action.

Should I use async controller actions?

Calling an asynchronous controller action will not block a thread in the thread pool. Asynchronous actions are best when your method is I/O, network-bound, or long-running and parallelizable. Another benefit of an asynchronous action is that it can be more easily canceled by the user than a synchronous request.

What is the benefit of using async?

with async / await , you write less code and your code will be more maintainable than using the previous asynchronous programming methods such as using plain tasks. async / await is the newer replacement to BackgroundWorker , which has been used on windows forms desktop applications.


1 Answers

The point of the await keyword is to let you work with asynchronous operations without writing ugly callbacks.

Using asynchronous operations helps avoid wasting thread pool threads.

Explanation

ASP.Net runs all of your code in threads from the managed thread pool.
If you have too many slow requests running at once, the thread pool will get full, and new requests will need to wait for a thread to get free.

Frequently, however, your requests are slow not because they're doing computation (compute-bound), but because they're waiting for something else, such as a hard disk, a database server, or an external webservice (IO- or network-bound).

There is no point in wasting a precious threadpool thread simply to wait for the external operation to finish.

Asynchronous operations allow you to start the operation, return your thread to the pool, then "wake up" on a different thread pool thread when the operation is finished.
While the operation is running, no threads are consumed.

like image 166
SLaks Avatar answered Oct 05 '22 05:10

SLaks