satish
satish

Reputation: 2443

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 .

Upvotes: 1

Views: 1868

Answers (1)

Stephen Cleary
Stephen Cleary

Reputation: 456417

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.

Upvotes: 5

Related Questions