1

When im running an Asp.net ( cs file) page :

and creating Thread T = new Thread(...) or using BeginInvoke()

Does the Thread is being taken from the Asp.net reserved Thread Pool ?

edit

Essentially, .NET maintains a pool of threads that can handle page requests. When a new request is received, ASP.NET grabs one of the available threads and uses it to process the entire page. That same thread instantiates the page, runs your event handling code, and returns the rendered HTML. If ASP.NET receives requests at a rapid pace—faster than it can serve them—unhandled requests will build up in a queue. If the queue fills up, ASP.NET is forced to reject additional requests with 503 “Server Unavailable” errors.

I dont want to impact the Asp.net "requests threads..."

enter image description here

Royi Namir
  • 144,742
  • 138
  • 468
  • 792
  • http://stackoverflow.com/questions/1325718/using-threadpool-queueuserworkitem-in-asp-net-in-a-high-traffic-scenario – Mitch Wheat May 08 '12 at 08:31

1 Answers1

4

When you use new Thread(), it actually creates a new thread that is not related to the ThreadPool.

When you use Delegate.BeginInvoke(), the delegate executes on the .Net ThreadPool (there is no special ASP.NET ThreadPool).

Usually, it's best if you use the ThreadPool for a short running tasks and create a thread manually if you need to run a long-running task. Another option is to use .Net 4.0 Task, which gives you a nicer, consistent API. CPU-bound Tasks usually run on the ThreaPool, but you can specify a LongRunning option, so that they create their own thread.

In general, you probably don't have to worry about starving the ThreadPool, even in ASP.NET applications, because the limits are high enough (at least in .Net 4.0, they were somewhat lower in previous versions). If you do encounter these problems, you can try increasing the number of thread in the ThreadPool, or you can use a separate thread pool (which will require some code, but you should be able to find code for this on the internet). Using a custom thread pool is easier if you used Tasks, because it means just switching a TaskScheduler.

svick
  • 236,525
  • 50
  • 385
  • 514
  • hi svick !:) please look at my edit. i guess i want to avboid problem as these... – Royi Namir May 08 '12 at 08:50
  • so i can use either new Thread || BeginInvoke - and it WONT hurt the Asp.net requests Thread.....right ? – Royi Namir May 08 '12 at 09:24
  • If you use `BeginInvoke`, it *might* hurt the ASP.NET request threads. But you probably shouldn't worry about it, unless it actually becomes a problem for you. – svick May 08 '12 at 09:33
  • svick and what about new Thread() ? will it hurt the ASP.NET request threads? – Royi Namir May 08 '12 at 10:24
  • @RoyiNamir, it won't use `ThreadPool` threads, so it won't cause the 503 error, if that's what you're asking. Wasn't that clear from my answer? – svick May 08 '12 at 10:32
  • so why would I ever want t use `Delegate.BeginInvoke()` ? I dont want a thread to be taken from the threadpool.....:) – Royi Namir Sep 11 '12 at 09:08
  • Well, you would use it if you wanted to run it on the ThreadPool. If you don't want that, don't use `Delegate.BeginInvoke()`. But I think that method has been mostly obsoleted by `Task`s. – svick Sep 11 '12 at 10:19