简体   繁体   中英

Making my MVC Async controller methods asynchronous so they can process at the same time

I have the following async code:

public class AsynchronousController : AsyncController
{
    public ActionResult IndexSynchronous(string city)
    {
        return View("Index");
    }

    public async Task<ActionResult> AsyncTest(string call1, string call2)
    {
        await Task.Delay(10000);
        return null;
    }
}

The goal is that I can simply open IndexSynchronous while AsyncTest is processing, but this way it does not work. I verified this by first calling Asynctest and then calling IndexSynchronous, which is still waiting for the other action.

Could anyone tell me what I am missing?

Async - Await is not about parallel programming. It is meant to avoid useless CPU processing while waiting for IO operations.

When you do:

string fileContent = await GetFileContent();

you're just telling the current thread to stop its execution until it receives an interupt (IO operation completed). Now tasks come in: when a thread which is executing a tasks awaits an IO operation to continue it can start executing another task while its waiting.

The degree of parallelism of a process is given by the number of thread of the threadpool that is using. Each thread than executes tasks. When a thread calls awaits stop the execution of the current task and starts executing another one.

Async - Await is about improving performance by avoiding thread to use CPU while waiting for IO operation

To answer your question, even in old ASP.NET web applications without async controllers you are able to process different requests (and so different controller actions) at the same time. You don't have to do anything to reach this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM