4

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.

asked Jun 22, 2016 at 12:41

2 Answers 2

7

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.

answered Jun 22, 2016 at 12:49

2 Comments

What if it is not a I/O operation. If it is a business logic which is executed within a function and consuming so much time. Wouldn't it create a new thread.
@Sunny: No. If you use async without await, then the code is just run directly on the calling thread. If you use await Task.Run, then the Task.Run (not the async/await) uses a separate thread pool thread (note: this is not recommended on ASP.NET).
1

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.

answered Jun 22, 2016 at 12:59

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.