简体   繁体   English

Asp.net MVC / WebAPI中的异步

[英]Async in Asp.net MVC/WebAPI

Using of Async and Await keyword is encourged in the recent conf with MS , If i use a Await keyword that can span a new thread in which the code will execute In which case what will happen to Asp.net Worker thread. 在最近与MS的conf中,鼓励使用Async和Await关键字,如果我使用的Await关键字可以跨越将在其中执行代码的新线程,在这种情况下,Asp.net Worker线程将如何处理。 Will it wait for the request to complete or will continue to serve other request . 它会等待请求完成还是继续为其他请求提供服务。

await does not spawn a new thread. await不会产生新线程。 See my async / await intro post or the async / await FAQ . 请参阅我的async / await简介介绍async / await常见问题解答

However, you can spawn a new task by explicitly calling Task.Run , if you want to. 但是,您可以通过显式调用Task.Run 生成新任务。

When you call await , if the object you're awaiting is not already complete, then await will schedule the remainder of your async method to run later, and the current thread is returned to the thread pool. 当您调用await ,如果您正在等待的对象尚未完成,那么await将安排您的async方法的其余部分在以后运行,并将当前线程返回到线程池中。 No threads are used by async or await while the asynchronous operation is in progress. 异步操作正在进行时, asyncawait过程不会使用任何线程。 When the asynchronous operation completes, the remainder of the async method runs on a thread pool thread (which may or may not be the same thread it started on). 异步操作完成后, async方法的其余部分在线程池线程上运行(该线程池可能与启动时所在的线程不同)。 This is not normally a problem because in the common case (1), the async method resumes in the correct request context. 这通常不是问题,因为在常见情况(1)中, async方法在正确的请求上下文中恢复。

(1) The common case is that await is awaiting a Task or Task<TResult> directly. (1)常见情况是await在等待TaskTask<TResult>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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