
Thread Pool Characteristics
Thread pool threads are background threads. See Foreground and Background Threads. Each thread uses the default stack size, runs at the default priority, and is in the multithreaded apartment.
There is only one thread pool per process.
Exceptions in Thread Pool Threads
Unhandled exceptions on thread pool threads terminate the process. There are three exceptions to this rule:
A ThreadAbortException is thrown in a thread pool thread, because Abort was called.
An AppDomainUnloadedException is thrown in a thread pool thread, because the application domain is being unloaded.
The common language runtime or a host process terminates the thread.
For more information, see Exceptions in Managed Threads.
Note: |
|---|
In the .NET Framework versions 1.0 and 1.1, the common language runtime silently traps unhandled exceptions in thread pool threads. This might corrupt application state and eventually cause applications to hang, which might be very difficult to debug.
|
Maximum Number of Thread Pool Threads
The number of operations that can be queued to the thread pool is limited only by available memory; however, the thread pool limits the number of threads that can be active in the process simultaneously. By default, the limit is 25 worker threads per CPU and 1,000 I/O completion threads.
You can control the maximum number of threads by using the GetMaxThreads and SetMaxThreads methods.
Note: |
|---|
In the .NET Framework versions 1.0 and 1.1, the size of the thread pool cannot be set from managed code. Code that hosts the common language runtime can set the size using
CorSetMaxThreads, defined in mscoree.h.
|
Minimum Number of Idle Threads
The thread pool also maintains a minimum number of available threads, even when all threads are idle, so that queued tasks can start immediately. Idle threads in excess of this minimum are terminated to save system resources. By default, one idle thread is maintained per processor.
The thread pool has a built-in delay (half a second in the .NET Framework version 2.0) before starting new idle threads. If your application periodically starts many tasks in a short time, a small increase in the number of idle threads can produce a significant increase in throughput. Setting the number of idle threads too high consumes system resources needlessly.
You can control the number of idle threads maintained by the thread pool by using the GetMinThreads and SetMinThreads methods.
Note: |
|---|
In the .NET Framework version 1.0, the minimum number of idle threads cannot be set.
|