简体   繁体   中英

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. Will it wait for the request to complete or will continue to serve other request .

await does not spawn a new thread. See my async / await intro post or the async / await FAQ .

However, you can spawn a new task by explicitly calling Task.Run , if you want to.

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. No threads are used by async or await while the asynchronous operation is in progress. 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). This is not normally a problem because in the common case (1), the async method resumes in the correct request context.

(1) The common case is that await is awaiting a Task or Task<TResult> directly.

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