Background threads in ASP.NET - what mechanism is best?

199 Views Asked by At

First off, I know the "correct" answer is don't do background threads in a ASP.NET web app. We have a good reason for doing it this way so please accept the postulate that it makes sense for our use case.

Our background processing is generally CPU bound (can be IO bound at times). And generally will take between 2 seconds and 2 minutes (and occasionally longer, as much as an hour). We will limit the number of background workers to 1.5x the number of cores on the server.

I also assume that it's best to use an existing thread pool in the ASP.NET environment. My code will limit the number of threads, but I'm guessing it's best to pull from the existing pool.

Which then leads to the question, what is the best way to create the threads?

  1. ThreadPool.QueueUserWorkItem
  2. HostingEnvironment.QueueBackgroundWorkItem()
  3. Task.Run()
  4. Something else?

And do HostingEnvironment.QueueBackgroundWorkItem() & Task.Run() come out of the ASP.NET thread pool?

0

There are 0 best solutions below