Search code examples
c#.netmultithreadingthreadpooltiming

How to avoid ThreadPool bottleneck when it runs out of threads


We have an application with some time constrains, say that we need to execute an action every 500 ms, it is a kind of watchdog so if we don't send a message before 500 ms bad things happen.

The application uses quite heavily the ThreadPool and this watchdog thing interacts with the ThreadPool.

We have found that on some low end machines and sometimes when we queue a new workitem it takes about 800ms to execute it so the watchdog fires. We guess it is related with the ThreadPool running out of threads / creating new ones.

Is there a way to avoid this like forcing the ThreadPool to create the threads in advance or in a different thread so the watchdog never has to wait until the ThreadPool can execute the request?


Solution

  • You can try using ThreadPool.SetMinThreads method.