ThreadPool does not start a new thread

I have a C # Windows service that runs various objects (class libraries). Each of these objects has its own "processing" logic, which launches several long-running processing threads using ThreadPool . I have one example, for example:

 System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(WorkerThread_Processing)); 

This works great. My application works without problems, and my threads work well.

Now, to test regression, I run the same objects up, but from a C # Console application, not from a Windows service. It calls the exact same code (because it calls the same objects), however the WorkerThread_Processing method WorkerThread_Processing delayed up to 20 seconds before running.

I went and switched from ThreadPool to Thread , and the problem disappeared. What could be here? I know that I am not older than MaxThreads count (I start 20 max threads).

+4
source share
2 answers

ThreadPool not specifically designed for long-running elements (more specifically, you don’t even have to start new threads when using ThreadPool , since its purpose is to distribute tasks in a limited number of threads).

If your task takes a long time, you must either break it down into logical partitions that fit into ThreadPool (or use the new Task infrastructure), or deploy your own Thread object.

Due to the fact that you are experiencing a delay, the MSDN documentation for the ThreadPool class says the following:

As part of a thread management strategy, a thread pool is delayed before thread creation. Therefore, when the number of tasks is queued for a short period of time, there may be a significant delay before all tasks begin.

You only know that ThreadPool not reached the maximum number of threads, and not the number of threads (if any), in fact it is sitting idle.

+13
source

The maximum number of thread threads is the maximum number that it can create. This is not the maximum number that has already been created. A thread pool has logic that prevents it from instantly deploying an entire thread chain.

If you name ThreadPool.QueueUserWorkItem 10 times in a row, the thread pool will not immediately create 10 threads. It will start the thread, delay, start another, etc.

I seem to remember that the delay was 500 milliseconds, but I can not find the documentation to verify this.

Here it is: Managed Thread Pool :

The thread pool has a built-in delay (half a second in the .NET Framework version 2.0) before starting new downtime. If your application periodically runs many tasks in a short time, a small increase in the number of idle threads can lead to a significant increase in throughput. Installing too many idle threads consumes system resources unnecessarily.

You can control the number of idle threads supported by a pool thread using GetMinThreads and SetMinThreads

Note that this quote is from the .NET 3.5 documentation version. Version 4.0 does not mention a delay.

+1
source

All Articles