Reputation: 4809
For long running operation, if the asp.net thread is freed up to server other requests. On which thread does the long running operation will get executed and how will it acquire asp.net thread upon its completion.
Upvotes: 4
Views: 543
Reputation: 1258
To answer your question, All threads are drawn from the thread pool.
An example scenario can be given like this, when a request is received by the server, an available thread is drawn from the pool to service the request.
Then you spawn a new thread (either by Async
or other means). Now a new thread is drawn from the pool to run the request of your Async
body.
Meanwhile the original thread is freed to the pool and goes back to process another request.
When your threading is finished, it fetches back another thread (may not be the same as original thread) from pool and completes your request.
It's a complete waste of time if this process is CPU bound since you are blocking one thread (which is from the same pool). However IO bound operations can be processed like this, since they use no threads.
Upvotes: 1
Reputation: 457342
As I describe on my blog, long-running I/O operations do not require a thread at all. Rather, they use naturally-asynchronous I/O, which does not require a thread.
Device drivers generally use DMA, and this allows the device to directly read/write out of the main system RAM. .NET complements this approach with an IOCP (I/O Completion Port) that is part of the thread pool, allowing a single thread (or very few threads) per appdomain to wait on huge numbers of I/O operations.
To answer the second half of your question, the asynchronous method will resume with the request context, but it might or might not be on the same thread it was on before the await
. The more common scenario is when the I/O operation completes, it signals the IOCP, which takes a thread pool thread to do a bit of housekeeping (marking the Task as complete, etc), and then that same thread enters the ASP.NET request context and resumes executing the handler. This doesn't always happen - sometimes a thread switch is necessary - but it's the most common case.
Upvotes: 7