System.Threading.ThreadPool
4.0.10.0
4.1.0.0
4.1.1.0
4.1.2.0
5.0.0.0
6.0.0.0
7.0.0.0
8.0.0.0
9.0.0.0
10.0.0.0
11.0.0.0
mscorlib
1.0.5000.0
2.0.0.0
2.0.5.0
4.0.0.0
netstandard
2.0.0.0
2.1.0.0
System.Object
[System.Runtime.CompilerServices.Nullable(0)]
[<System.Runtime.CompilerServices.Nullable(0)>]
Provides a pool of threads that can be used to execute tasks, post work items, process asynchronous I/O, wait on behalf of other threads, and process timers.
or object to perform some task asynchronously, by default the task is scheduled to run on a thread pool thread.
- Asynchronous timers use the thread pool. Thread pool threads execute callbacks from the class and raise events from the class.
- When you use registered wait handles, a system thread monitors the status of the wait handles. When a wait operation completes, a worker thread from the thread pool executes the corresponding callback function.
- When you call the method to queue a method for execution on a thread pool thread. You do this by passing the method a delegate. The delegate has the signature
```csharp
void WaitCallback(Object state)
```
```vb
Sub WaitCallback(state As Object)
```
where `state` is an object that contains data to be used by the delegate. The actual data can be passed to the delegate by calling the method.
> [!NOTE]
> The threads in the managed thread pool are background threads. That is, their properties are `true`. This means that a thread will not keep an application running after all foreground threads have exited.
> [!IMPORTANT]
> When the thread pool reuses a thread, it does not clear the data in thread local storage or in fields that are marked with the attribute. Therefore, when a method examines thread local storage or fields that are marked with the attribute, the values it finds might be left over from an earlier use of the thread pool thread.
You can also queue work items that are not related to a wait operation to the thread pool. To request that a work item be handled by a thread in the thread pool, call the method. This method takes as a parameter a reference to the method or delegate that will be called by the thread selected from the thread pool. There is no way to cancel a work item after it has been queued.
Timer-queue timers and registered wait operations also use the thread pool. Their callback functions are queued to the thread pool.
There is one thread pool per process. Beginning with the .NET Framework 4, the default size of the thread pool for a process depends on several factors, such as the size of the virtual address space. A process can call the method to determine the number of threads. The number of threads in the thread pool can be changed by using the method. Each thread uses the default stack size and runs at the default priority.
> [!NOTE]
> Unmanaged code that hosts the .NET Framework can change the size of the thread pool by using the `CorSetMaxThreads` function, defined in the mscoree.h file.
The thread pool provides new worker threads or I/O completion threads on demand until it reaches the maximum for each category. When a maximum is reached, the thread pool can create additional threads in that category or wait until some tasks complete. Beginning with the .NET Framework 4, the thread pool creates and destroys worker threads in order to optimize throughput, which is defined as the number of tasks that complete per unit of time. Too few threads might not make optimal use of available resources, whereas too many threads could increase resource contention.
> [!NOTE]
> When demand is low, the actual number of thread pool threads can fall below the minimum values.
You can use the method to obtain these minimum values.
> [!CAUTION]
> You can use the method to increase the minimum number of threads. However, unnecessarily increasing these values can cause performance problems. If too many tasks start at the same time, all of them might appear to be slow. In most cases the thread pool will perform better with its own algorithm for allocating threads.
## Examples
In the following example, the main application thread queues a method named `ThreadProc` to execute on a thread pool thread, sleeps for one second, and then exits. The `ThreadProc` method simply displays a message.
:::code language="csharp" source="~/snippets/csharp/System.Threading/ThreadPool/Overview/source.cs" id="Snippet1":::
:::code language="vb" source="~/snippets/visualbasic/System.Threading/ThreadPool/Overview/source.vb" id="Snippet1":::
If you comment out the call to the method, the main thread exits before method runs on the thread pool thread. The thread pool uses background threads, which do not keep the application running if all foreground threads have terminated. (This is a simple example of a race condition.)
]]>
This type is thread safe.
Threads and Threading
The Managed Thread Pooling
mscorlib
2.0.5.0
4.0.0.0
Binds an operating system handle to the .
Method
mscorlib
1.0.5000.0
2.0.0.0
2.0.5.0
4.0.0.0
netstandard
2.0.0.0
2.1.0.0
System.Threading.ThreadPool
4.1.0.0
4.1.1.0
4.1.2.0
5.0.0.0
6.0.0.0
7.0.0.0
8.0.0.0
9.0.0.0
10.0.0.0
11.0.0.0
[System.Obsolete("ThreadPool.BindHandle(IntPtr) has been deprecated. Use ThreadPool.BindHandle(SafeHandle) instead.")]
[<System.Obsolete("ThreadPool.BindHandle(IntPtr) has been deprecated. Use ThreadPool.BindHandle(SafeHandle) instead.")>]
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
[<System.Runtime.Versioning.SupportedOSPlatform("windows")>]
[System.Obsolete("ThreadPool.BindHandle(IntPtr) has been deprecated. Please use ThreadPool.BindHandle(SafeHandle) instead.", false)]
[<System.Obsolete("ThreadPool.BindHandle(IntPtr) has been deprecated. Please use ThreadPool.BindHandle(SafeHandle) instead.", false)>]
[System.Security.SecuritySafeCritical]
[<System.Security.SecuritySafeCritical>]
System.Boolean
An that holds the handle. The handle must have been opened for overlapped I/O on the unmanaged side.
Binds an operating system handle to the .
if the handle is bound; otherwise, .
class.
]]>
The caller does not have the required permission.
Method
System.Threading.ThreadPool
4.0.10.0
4.1.0.0
4.1.1.0
4.1.2.0
5.0.0.0
6.0.0.0
7.0.0.0
8.0.0.0
9.0.0.0
10.0.0.0
11.0.0.0
mscorlib
2.0.0.0
2.0.5.0
4.0.0.0
netstandard
2.0.0.0
2.1.0.0
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
[<System.Runtime.Versioning.SupportedOSPlatform("windows")>]
[System.Security.SecurityCritical]
[<System.Security.SecurityCritical>]
[System.Security.SecuritySafeCritical]
[<System.Security.SecuritySafeCritical>]
System.Boolean
A that holds the operating system handle. The handle must have been opened for overlapped I/O on the unmanaged side.
Binds an operating system handle to the .
if the handle is bound; otherwise, .
class.
The `osHandle` parameter should be a , which derives from the abstract class.
]]>
is .
Property
System.Threading.ThreadPool
4.1.1.0
4.1.2.0
5.0.0.0
6.0.0.0
7.0.0.0
8.0.0.0
9.0.0.0
10.0.0.0
11.0.0.0
netstandard
System.Int64
Gets the number of work items that have been processed so far.
The number of work items that have been processed so far.
Method
mscorlib
1.0.5000.0
2.0.0.0
2.0.5.0
4.0.0.0
netstandard
2.0.0.0
2.1.0.0
System.Threading.ThreadPool
4.1.0.0
4.1.1.0
4.1.2.0
5.0.0.0
6.0.0.0
7.0.0.0
8.0.0.0
9.0.0.0
10.0.0.0
11.0.0.0
[System.Security.SecuritySafeCritical]
[<System.Security.SecuritySafeCritical>]
System.Void
The number of available worker threads.
The number of available asynchronous I/O threads.
Retrieves the difference between the maximum number of thread pool threads returned by the method, and the number currently active.
returns, the variable specified by `workerThreads` contains the number of additional worker threads that can be started, and the variable specified by `completionPortThreads` contains the number of additional asynchronous I/O threads that can be started.
If there are no available threads, additional thread pool requests remain queued until thread pool threads become available.
## Examples
The following example displays the number of worker threads and I/O threads available when a simple app is started.
:::code language="csharp" source="~/snippets/csharp/System.Threading/ThreadPool/GetAvailableThreads/Example2.cs" id="Snippet2":::
:::code language="vb" source="~/snippets/visualbasic/System.Threading/ThreadPool/GetAvailableThreads/Example2.vb" id="Snippet2":::
]]>
Method
mscorlib
1.0.5000.0
2.0.0.0
2.0.5.0
4.0.0.0
netstandard
2.0.0.0
2.1.0.0
System.Threading.ThreadPool
4.1.0.0
4.1.1.0
4.1.2.0
5.0.0.0
6.0.0.0
7.0.0.0
8.0.0.0
9.0.0.0
10.0.0.0
11.0.0.0
[System.Security.SecuritySafeCritical]
[<System.Security.SecuritySafeCritical>]
System.Void
The maximum number of worker threads in the thread pool.
The maximum number of asynchronous I/O threads in the thread pool.
Retrieves the number of requests to the thread pool that can be active concurrently. All requests above that number remain queued until thread pool threads become available.
returns, the variable specified by `workerThreads` contains the maximum number of worker threads allowed in the thread pool, and the variable specified by `completionPortThreads` contains the maximum number of asynchronous I/O threads allowed in the thread pool.
You can use the method to determine the actual number of threads in the thread pool at any given time.
You can use the to set the maximum number of worker threads and asynchronous I/O threads in the thread pool.
You can queue as many thread pool requests as system memory allows. If there are more requests than thread pool threads, the additional requests remain queued until thread pool threads become available.
## Examples
The following code example shows how to retrieve a count of the maximum and available number of threads in the thread pool. A work item is queued that uses `FileStream` to asynchronously write to two files. The callback methods are timed to overlap. A worker thread handles the work item and, depending on the speed and number of processors on the computer, one or two completion port threads handle the write operations.
:::code language="csharp" source="~/snippets/csharp/System.Threading/ThreadPool/GetAvailableThreads/source.cs" id="Snippet1":::
:::code language="vb" source="~/snippets/visualbasic/System.Threading/ThreadPool/GetAvailableThreads/source.vb" id="Snippet1":::
]]>
Method
mscorlib
1.0.5000.0
2.0.0.0
2.0.5.0
4.0.0.0
netstandard
2.0.0.0
2.1.0.0
System.Threading.ThreadPool
4.1.0.0
4.1.1.0
4.1.2.0
5.0.0.0
6.0.0.0
7.0.0.0
8.0.0.0
9.0.0.0
10.0.0.0
11.0.0.0
[System.Security.SecuritySafeCritical]
[<System.Security.SecuritySafeCritical>]
System.Void
When this method returns, contains the minimum number of worker threads that the thread pool creates on demand.
When this method returns, contains the minimum number of asynchronous I/O threads that the thread pool creates on demand.
Retrieves the minimum number of threads the thread pool creates on demand, as new requests are made, before switching to an algorithm for managing thread creation and destruction.
[!NOTE]
> When demand is low, the actual number of thread pool threads can fall below the minimum values.
## Examples
The following example sets the minimum number of worker threads to four, and preserves the original value for the minimum number of asynchronous I/O completion threads.
:::code language="csharp" source="~/snippets/csharp/System.Threading/ThreadPool/GetMinThreads/source.cs" id="Snippet1":::
:::code language="vb" source="~/snippets/visualbasic/System.Threading/ThreadPool/GetMinThreads/source.vb" id="Snippet1":::
]]>
Property
System.Threading.ThreadPool
4.1.1.0
4.1.2.0
5.0.0.0
6.0.0.0
7.0.0.0
8.0.0.0
9.0.0.0
10.0.0.0
11.0.0.0
netstandard
System.Int64
Gets the number of work items that are currently queued to be processed.
The number of work items that are currently queued to be processed.
System.Threading.ThreadPool
4.0.10.0
4.1.0.0
Queues a method for execution. The method executes when a thread pool thread becomes available.
Method
System.Threading.ThreadPool
4.0.10.0
4.1.0.0
4.1.1.0
4.1.2.0
5.0.0.0
6.0.0.0
7.0.0.0
8.0.0.0
9.0.0.0
10.0.0.0
11.0.0.0
mscorlib
1.0.5000.0
2.0.0.0
2.0.5.0
4.0.0.0
netstandard
2.0.0.0
2.1.0.0
[System.Security.SecuritySafeCritical]
[<System.Security.SecuritySafeCritical>]
System.Boolean
A that represents the method to be executed.
Queues a method for execution. The method executes when a thread pool thread becomes available.
if the method is successfully queued; is thrown if the work item could not be queued.
overload that accepts an object containing the necessary data.
> [!NOTE]
> Visual Basic users can omit the constructor, and simply use the `AddressOf` operator when passing the callback method to . Visual Basic automatically calls the correct delegate constructor.
The property value is propagated to worker threads queued using the method.
]]>
method overload to queue a task, which is represented by the `ThreadProc` method, to execute when a thread becomes available. No task information is supplied with this overload. Therefore, the information that is available to the `ThreadProc` method is limited to the object the method belongs to.
:::code language="csharp" source="~/snippets/csharp/System.Threading/ThreadPool/Overview/source.cs" id="Snippet1":::
:::code language="vb" source="~/snippets/visualbasic/System.Threading/ThreadPool/Overview/source.vb" id="Snippet1":::
]]>
is .
The common language runtime (CLR) is hosted, and the host does not support this action.
The Managed Thread Pool
Method
System.Threading.ThreadPool
4.0.10.0
4.1.0.0
4.1.1.0
4.1.2.0
5.0.0.0
6.0.0.0
7.0.0.0
8.0.0.0
9.0.0.0
10.0.0.0
11.0.0.0
mscorlib
1.0.5000.0
2.0.0.0
2.0.5.0
4.0.0.0
netstandard
2.0.0.0
2.1.0.0
[System.Security.SecuritySafeCritical]
[<System.Security.SecuritySafeCritical>]
System.Boolean
[System.Runtime.CompilerServices.Nullable(2)]
[<System.Runtime.CompilerServices.Nullable(2)>]
A representing the method to execute.
An object containing data to be used by the method.
Queues a method for execution, and specifies an object containing data to be used by the method. The method executes when a thread pool thread becomes available.
if the method is successfully queued; is thrown if the work item could not be queued.
[!NOTE]
> Visual Basic users can omit the constructor, and simply use the `AddressOf` operator when passing the callback method to . Visual Basic automatically calls the correct delegate constructor.
## Examples
The following example uses the .NET thread pool to calculate the `Fibonacci` result for five numbers between 20 and 40. Each `Fibonacci` result is represented by the `Fibonacci` class, which provides a method named `ThreadPoolCallback` that performs the calculation. An object that represents each `Fibonacci` value is created, and the `ThreadPoolCallback` method is passed to , which assigns an available thread in the pool to execute the method.
Because each `Fibonacci` object is given a semi-random value to compute, and because each thread will be competing for processor time, you cannot know in advance how long it will take for all five results to be calculated. That is why each `Fibonacci` object is passed an instance of the class during construction. Each object signals the provided event object when its calculation is complete, which allows the primary thread to block execution with until all five `Fibonacci` objects have calculated a result. The `Main` method then displays each `Fibonacci` result.
:::code language="csharp" source="~/snippets/csharp/System.Threading/ThreadPool/QueueUserWorkItem/source.cs" id="Snippet1":::
:::code language="vb" source="~/snippets/visualbasic/System.Threading/ThreadPool/QueueUserWorkItem/source.vb" id="Snippet1":::
]]>
The common language runtime (CLR) is hosted, and the host does not support this action.
is .
The Managed Thread Pool
Method
System.Threading.ThreadPool
4.1.1.0
4.1.2.0
5.0.0.0
6.0.0.0
7.0.0.0
8.0.0.0
9.0.0.0
10.0.0.0
11.0.0.0
netstandard
2.1.0.0
System.Boolean
[System.Runtime.CompilerServices.Nullable(2)]
[<System.Runtime.CompilerServices.Nullable(2)>]
The type of elements of .
An representing the method to execute.
An object containing data to be used by the method.
to prefer queueing the work item in a queue close to the current thread; to prefer queueing the work item to the thread pool's shared queue.
Queues a method specified by an delegate for execution, and provides data to be used by the method. The method executes when a thread pool thread becomes available.
if the method is successfully queued; is thrown if the work item could not be queued.
To be added.
System.Threading.ThreadPool
4.0.10.0
4.1.0.0
Registers a delegate that is waiting for a .
Method
System.Threading.ThreadPool
4.0.10.0
4.1.0.0
4.1.1.0
4.1.2.0
5.0.0.0
6.0.0.0
7.0.0.0
8.0.0.0
9.0.0.0
10.0.0.0
11.0.0.0
mscorlib
1.0.5000.0
2.0.0.0
2.0.5.0
4.0.0.0
netstandard
2.0.0.0
2.1.0.0
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
[<System.Runtime.Versioning.UnsupportedOSPlatform("browser")>]
[System.Security.SecuritySafeCritical]
[<System.Security.SecuritySafeCritical>]
System.Threading.RegisteredWaitHandle
[System.Runtime.CompilerServices.Nullable(2)]
[<System.Runtime.CompilerServices.Nullable(2)>]
The to register. Use a other than .
The delegate to call when the parameter is signaled.
The object that is passed to the delegate.
The time-out in milliseconds. If the parameter is 0 (zero), the function tests the object's state and returns immediately. If is -1, the function's time-out interval never elapses.
to indicate that the thread will no longer wait on the parameter after the delegate has been called; to indicate that the timer is reset every time the wait operation completes until the wait is unregistered.
Registers a delegate to wait for a , specifying a 32-bit signed integer for the time-out in milliseconds.
The that encapsulates the native handle.
that is returned by this method, call its method to release references to the wait handle. We recommend that you always call the method, even if you specify `true` for `executeOnlyOnce`. Garbage collection works more efficiently if you call the method instead of depending on the registered wait handle's finalizer.
The method queues the specified delegate to the thread pool. A worker thread will execute the delegate when one of the following occurs:
- The specified object is in the signaled state.
- The time-out interval elapses.
The method checks the current state of the specified object's . If the object's state is unsignaled, the method registers a wait operation. The wait operation is performed by a thread from the thread pool. The delegate is executed by a worker thread when the object's state becomes signaled or the time-out interval elapses. If the `timeOutInterval` parameter is not 0 (zero) and the `executeOnlyOnce` parameter is `false`, the timer is reset every time the event is signaled or the time-out interval elapses.
> [!IMPORTANT]
> Using a for `waitObject` does not provide mutual exclusion for the callbacks because the underlying Windows API uses the default `WT_EXECUTEDEFAULT` flag, so each callback is dispatched on a separate thread pool thread. Instead of a , use a with a maximum count of 1.
To cancel the wait operation, call the method.
The wait thread uses the Win32 `WaitForMultipleObjects` function to monitor registered wait operations. Therefore, if you must use the same native operating system handle in multiple calls to , you must duplicate the handle using the Win32 `DuplicateHandle` function. Note that you should not pulse an event object passed to , because the wait thread might not detect that the event is signaled before it is reset.
Before returning, the function modifies the state of some types of synchronization objects. Modification occurs only for the object whose signaled state caused the wait condition to be satisfied. For example, the count of a semaphore is decreased by one.
]]>
The parameter is less than -1.
The Managed Thread Pool
Method
System.Threading.ThreadPool
4.0.10.0
4.1.0.0
4.1.1.0
4.1.2.0
5.0.0.0
6.0.0.0
7.0.0.0
8.0.0.0
9.0.0.0
10.0.0.0
11.0.0.0
mscorlib
1.0.5000.0
2.0.0.0
2.0.5.0
4.0.0.0
netstandard
2.0.0.0
2.1.0.0
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
[<System.Runtime.Versioning.UnsupportedOSPlatform("browser")>]
[System.Security.SecuritySafeCritical]
[<System.Security.SecuritySafeCritical>]
System.Threading.RegisteredWaitHandle
[System.Runtime.CompilerServices.Nullable(2)]
[<System.Runtime.CompilerServices.Nullable(2)>]
The to register. Use a other than .
The delegate to call when the parameter is signaled.
The object passed to the delegate.
The time-out in milliseconds. If the parameter is 0 (zero), the function tests the object's state and returns immediately. If is -1, the function's time-out interval never elapses.
to indicate that the thread will no longer wait on the parameter after the delegate has been called; to indicate that the timer is reset every time the wait operation completes until the wait is unregistered.
Registers a delegate to wait for a , specifying a 64-bit signed integer for the time-out in milliseconds.
The that encapsulates the native handle.
that is returned by this method, call its method to release references to the wait handle. We recommend that you always call the method, even if you specify `true` for `executeOnlyOnce`. Garbage collection works more efficiently if you call the method instead of depending on the registered wait handle's finalizer.
The method queues the specified delegate to the thread pool. A worker thread will execute the delegate when one of the following occurs:
- The specified object is in the signaled state.
- The time-out interval elapses.
The method checks the current state of the specified object's . If the object's state is unsignaled, the method registers a wait operation. The wait operation is performed by a thread from the thread pool. The delegate is executed by a worker thread when the object's state becomes signaled or the time-out interval elapses. If the `timeOutInterval` parameter is not 0 (zero) and the `executeOnlyOnce` parameter is `false`, the timer is reset every time the event is signaled or the time-out interval elapses.
> [!IMPORTANT]
> Using a for `waitObject` does not provide mutual exclusion for the callbacks because the underlying Windows API uses the default `WT_EXECUTEDEFAULT` flag, so each callback is dispatched on a separate thread pool thread. Instead of a , use a with a maximum count of 1.
To cancel the wait operation, call the method.
The wait thread uses the Win32 `WaitForMultipleObjects` function to monitor registered wait operations. Therefore, if you must use the same native operating system handle in multiple calls to , you must duplicate the handle using the Win32 `DuplicateHandle` function. Note that you should not pulse an event object passed to , because the wait thread might not detect that the event is signaled before it is reset.
Before returning, the function modifies the state of some types of synchronization objects. Modification occurs only for the object whose signaled state caused the wait condition to be satisfied. For example, the count of a semaphore is decreased by one.
]]>
The parameter is less than -1.
The Managed Thread Pool
Method
System.Threading.ThreadPool
4.0.10.0
4.1.0.0
4.1.1.0
4.1.2.0
5.0.0.0
6.0.0.0
7.0.0.0
8.0.0.0
9.0.0.0
10.0.0.0
11.0.0.0
mscorlib
1.0.5000.0
2.0.0.0
2.0.5.0
4.0.0.0
netstandard
2.0.0.0
2.1.0.0
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
[<System.Runtime.Versioning.UnsupportedOSPlatform("browser")>]
[System.Security.SecuritySafeCritical]
[<System.Security.SecuritySafeCritical>]
System.Threading.RegisteredWaitHandle
[System.Runtime.CompilerServices.Nullable(2)]
[<System.Runtime.CompilerServices.Nullable(2)>]
The to register. Use a other than .
The delegate to call when the parameter is signaled.
The object passed to the delegate.
The time-out represented by a . If is 0 (zero), the function tests the object's state and returns immediately. If is -1, the function's time-out interval never elapses.
to indicate that the thread will no longer wait on the parameter after the delegate has been called; to indicate that the timer is reset every time the wait operation completes until the wait is unregistered.
Registers a delegate to wait for a , specifying a value for the time-out.
The that encapsulates the native handle.
that is returned by this method, call its method to release references to the wait handle. We recommend that you always call the method, even if you specify `true` for `executeOnlyOnce`. Garbage collection works more efficiently if you call the method instead of depending on the registered wait handle's finalizer.
The method queues the specified delegate to the thread pool. A worker thread will execute the delegate when one of the following occurs:
- The specified object is in the signaled state.
- The time-out interval elapses.
The method checks the current state of the specified object's . If the object's state is unsignaled, the method registers a wait operation. The wait operation is performed by a thread from the thread pool. The delegate is executed by a worker thread when the object's state becomes signaled or the time-out interval elapses. If the `timeOutInterval` parameter is not 0 (zero) and the `executeOnlyOnce` parameter is `false`, the timer is reset every time the event is signaled or the time-out interval elapses.
> [!IMPORTANT]
> Using a for `waitObject` does not provide mutual exclusion for the callbacks because the underlying Windows API uses the default `WT_EXECUTEDEFAULT` flag, so each callback is dispatched on a separate thread pool thread. Instead of a , use a with a maximum count of 1.
To cancel the wait operation, call the method.
The wait thread uses the Win32 `WaitForMultipleObjects` function to monitor registered wait operations. Therefore, if you must use the same native operating system handle in multiple calls to , you must duplicate the handle using the Win32 `DuplicateHandle` function. Note that you should not pulse an event object passed to , because the wait thread might not detect that the event is signaled before it is reset.
Before returning, the function modifies the state of some types of synchronization objects. Modification occurs only for the object whose signaled state caused the wait condition to be satisfied. For example, the count of a semaphore is decreased by one.
]]>
The parameter is less than -1.
The parameter is greater than Int32.MaxValue.
The Managed Thread Pool
Method
System.Threading.ThreadPool
4.0.10.0
4.1.0.0
4.1.1.0
4.1.2.0
5.0.0.0
6.0.0.0
7.0.0.0
8.0.0.0
9.0.0.0
10.0.0.0
11.0.0.0
mscorlib
1.0.5000.0
2.0.0.0
2.0.5.0
4.0.0.0
netstandard
2.0.0.0
2.1.0.0
[System.CLSCompliant(false)]
[<System.CLSCompliant(false)>]
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
[<System.Runtime.Versioning.UnsupportedOSPlatform("browser")>]
[System.Security.SecuritySafeCritical]
[<System.Security.SecuritySafeCritical>]
System.Threading.RegisteredWaitHandle
[System.Runtime.CompilerServices.Nullable(2)]
[<System.Runtime.CompilerServices.Nullable(2)>]
The to register. Use a other than .
The delegate to call when the parameter is signaled.
The object passed to the delegate.
The time-out in milliseconds. If the parameter is 0 (zero), the function tests the object's state and returns immediately. If is -1, the function's time-out interval never elapses.
to indicate that the thread will no longer wait on the parameter after the delegate has been called; to indicate that the timer is reset every time the wait operation completes until the wait is unregistered.
Registers a delegate to wait for a , specifying a 32-bit unsigned integer for the time-out in milliseconds.
The that can be used to cancel the registered wait operation.
that is returned by this method, call its method to release references to the wait handle. We recommend that you always call the method, even if you specify `true` for `executeOnlyOnce`. Garbage collection works more efficiently if you call the method instead of depending on the registered wait handle's finalizer.
The method queues the specified delegate to the thread pool. A worker thread will execute the delegate when one of the following occurs:
- The specified object is in the signaled state.
- The time-out interval elapses.
The method checks the current state of the specified object's . If the object's state is unsignaled, the method registers a wait operation. The wait operation is performed by a thread from the thread pool. The delegate is executed by a worker thread when the object's state becomes signaled or the time-out interval elapses. If the `timeOutInterval` parameter is not 0 (zero) and the `executeOnlyOnce` parameter is `false`, the timer is reset every time the event is signaled or the time-out interval elapses.
> [!IMPORTANT]
> Using a for `waitObject` does not provide mutual exclusion for the callbacks because the underlying Windows API uses the default `WT_EXECUTEDEFAULT` flag, so each callback is dispatched on a separate thread pool thread. Instead of a , use a with a maximum count of 1.
To cancel the wait operation, call the method.
The wait thread uses the Win32 `WaitForMultipleObjects` function to monitor registered wait operations. Therefore, if you must use the same native operating system handle in multiple calls to , you must duplicate the handle using the Win32 `DuplicateHandle` function. Note that you should not pulse an event object passed to , because the wait thread might not detect that the event is signaled before it is reset.
Before returning, the function modifies the state of some types of synchronization objects. Modification occurs only for the object whose signaled state caused the wait condition to be satisfied. For example, the count of a semaphore is decreased by one.
## Examples
The following example shows how to use the method to execute a specified callback method when a specified wait handle is signaled. In this example, the callback method is `WaitProc`, and the wait handle is an .
The example defines a `TaskInfo` class to hold the information that is passed to the callback when it executes. The example creates a `TaskInfo` object and assigns it some string data. The that is returned by the method is assigned to the `Handle` field of the `TaskInfo` object so that the callback method has access to the .
In addition to specifying `TaskInfo` as the object to pass to the callback method, the call to the method specifies the that the task will wait for, a delegate that represents the `WaitProc` callback method, a one second time-out interval, and multiple callbacks.
When the main thread signals the by calling its method, the delegate is invoked. The `WaitProc` method tests to determine whether a time-out occurred. If the callback was invoked because the wait handle was signaled, the `WaitProc` method unregisters the , stopping additional callbacks. In the case of a time-out, the task continues to wait. The `WaitProc` method ends by printing a message to the console.
:::code language="csharp" source="~/snippets/csharp/System.Threading/RegisteredWaitHandle/Overview/source.cs" id="Snippet1":::
:::code language="vb" source="~/snippets/visualbasic/System.Threading/RegisteredWaitHandle/Overview/source.vb" id="Snippet1":::
]]>
The parameter is less than -1.
The Managed Thread Pool
Method
mscorlib
2.0.0.0
2.0.5.0
4.0.0.0
netstandard
2.0.0.0
2.1.0.0
System.Threading.ThreadPool
4.1.0.0
4.1.1.0
4.1.2.0
5.0.0.0
6.0.0.0
7.0.0.0
8.0.0.0
9.0.0.0
10.0.0.0
11.0.0.0
[System.Security.SecuritySafeCritical]
[<System.Security.SecuritySafeCritical>]
System.Boolean
The maximum number of worker threads in the thread pool.
The maximum number of asynchronous I/O threads in the thread pool.
Sets the number of requests to the thread pool that can be active concurrently. All requests above that number remain queued until thread pool threads become available.
if the change is successful; otherwise, .
property. In addition, you cannot set the maximum number of worker threads or I/O completion threads to a number smaller than the corresponding minimum number of worker threads or I/O completion threads. To determine the minimum thread pool size, call the method.
If the common language runtime is hosted, for example by Internet Information Services (IIS) or SQL Server, the host can limit or prevent changes to the thread pool size.
Use caution when changing the maximum number of threads in the thread pool. While your code might benefit, the changes might have an adverse effect on code libraries you use.
Setting the thread pool size too large can cause performance problems. If too many threads are executing at the same time, the task switching overhead becomes a significant factor.
> [!NOTE]
> The thread pool may have upper limits for the maximum thread counts (such as `short.MaxValue`, depending on the implementation). The argument values are capped to the upper limit, so even when the method returns `true`, the actual maximum thread counts may be lower than requested.
]]>
Method
mscorlib
1.0.5000.0
2.0.0.0
2.0.5.0
4.0.0.0
netstandard
2.0.0.0
2.1.0.0
System.Threading.ThreadPool
4.1.0.0
4.1.1.0
4.1.2.0
5.0.0.0
6.0.0.0
7.0.0.0
8.0.0.0
9.0.0.0
10.0.0.0
11.0.0.0
[System.Security.SecuritySafeCritical]
[<System.Security.SecuritySafeCritical>]
System.Boolean
The minimum number of worker threads that the thread pool creates on demand.
The minimum number of asynchronous I/O threads that the thread pool creates on demand.
Sets the minimum number of threads the thread pool creates on demand, as new requests are made, before switching to an algorithm for managing thread creation and destruction.
if the change is successful; otherwise, .
), returns `false` and does not change either of the minimum values.
By default, the minimum number of threads is set to the processor count. You can use to increase the minimum number of threads, such as to temporarily work around issues where some queued work items or tasks block thread pool threads. Those blockages sometimes lead to a situation where all worker or I/O completion threads are blocked (starvation). However, increasing the minimum number of threads might degrade performance in other ways, such as:
- The thread pool may schedule more worker threads, even when the worker threads are not getting blocked. The oversubscription can cause threads that get scheduled-out to be significantly delayed as they wait in a long queue to get another time slice, delaying some work items or tasks.
- Worker threads may take more CPU time in dequeuing work items due to having to scan more threads to steal work from.
- Context switching between threads may increase CPU usage.
- Garbage collection may take more CPU time in thread stack walking.
- The process may consume more memory.
> [!CAUTION]
> Using the method to increase the minimum number of threads can cause performance problems as described in the preceding text. In most cases, the thread pool will perform better with its own algorithm for allocating threads. Reducing the minimum to less than the number of processors can also hurt performance.
## Examples
The following example sets the minimum number of worker threads to four, and preserves the original value for the minimum number of asynchronous I/O completion threads.
:::code language="csharp" source="~/snippets/csharp/System.Threading/ThreadPool/GetMinThreads/source.cs" id="Snippet1":::
:::code language="vb" source="~/snippets/visualbasic/System.Threading/ThreadPool/GetMinThreads/source.vb" id="Snippet1":::
]]>
Property
System.Threading.ThreadPool
4.1.1.0
4.1.2.0
5.0.0.0
6.0.0.0
7.0.0.0
8.0.0.0
9.0.0.0
10.0.0.0
11.0.0.0
netstandard
System.Int32
Gets the number of thread pool threads that currently exist.
The number of thread pool threads that currently exist.
Method
mscorlib
2.0.0.0
2.0.5.0
4.0.0.0
netstandard
2.0.0.0
2.1.0.0
System.Threading.ThreadPool
4.1.0.0
4.1.1.0
4.1.2.0
5.0.0.0
6.0.0.0
7.0.0.0
8.0.0.0
9.0.0.0
10.0.0.0
11.0.0.0
[System.CLSCompliant(false)]
[<System.CLSCompliant(false)>]
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
[<System.Runtime.Versioning.SupportedOSPlatform("windows")>]
[System.Security.SecurityCritical]
[<System.Security.SecurityCritical>]
System.Boolean
The structure to queue.
Queues an overlapped I/O operation for execution.
if the operation was successfully queued to an I/O completion port; otherwise, .
class, the structure, and the `OVERLAPPED` structure in the Win32 Platform SDK.
> [!CAUTION]
> Using the method could inadvertently open up a security hole. Code access security bases its permission checks on the permissions of all the callers on the stack. When work is queued on a thread pool thread using , the stack of the thread pool thread does not have the context of the actual callers. Malicious code might be able exploit this to avoid permission checks.
]]>
Method
System.Threading.ThreadPool
4.1.1.0
4.1.2.0
5.0.0.0
6.0.0.0
7.0.0.0
8.0.0.0
9.0.0.0
10.0.0.0
11.0.0.0
netstandard
System.Boolean
The work item to invoke when a thread in the thread pool picks up the work item.
to prefer queueing the work item in a queue close to the current thread; to prefer queueing the work item to the thread pool's shared queue.
Queues the specified work item object to the thread pool.
if the method succeeds; is thrown if the work item could not be queued.
method. It is the responsibility of that work item to propagate if it's needed; the thread pool will not do so.
]]>
is .
The work item is a .
The work item could not be queued.
Method
mscorlib
1.0.5000.0
2.0.0.0
2.0.5.0
4.0.0.0
netstandard
2.0.0.0
2.1.0.0
System.Threading.ThreadPool
4.1.0.0
4.1.1.0
4.1.2.0
5.0.0.0
6.0.0.0
7.0.0.0
8.0.0.0
9.0.0.0
10.0.0.0
11.0.0.0
[System.Security.SecurityCritical]
[<System.Security.SecurityCritical>]
System.Boolean
[System.Runtime.CompilerServices.Nullable(2)]
[<System.Runtime.CompilerServices.Nullable(2)>]
A that represents the delegate to invoke when a thread in the thread pool picks up the work item.
The object that is passed to the delegate when serviced from the thread pool.
Queues the specified delegate to the thread pool, but does not propagate the calling stack to the worker thread.
if the method succeeds; is thrown if the work item could not be queued.
method, does not propagate the calling stack to the worker thread. This allows code to lose the calling stack and thereby to elevate its security privileges.
> [!CAUTION]
> Using could inadvertently open up a security hole. Code access security bases its permission checks on the permissions of all the callers on the stack. When work is queued on a thread pool thread using , the stack of the thread pool thread will not have the context of the actual callers. Malicious code might be able exploit this to avoid permission checks.
]]>
The caller does not have the required permission.
An out-of-memory condition was encountered.
The work item could not be queued.
is .
The Managed Thread Pool
Method
System.Threading.ThreadPool
4.1.1.0
4.1.2.0
5.0.0.0
6.0.0.0
7.0.0.0
8.0.0.0
9.0.0.0
10.0.0.0
11.0.0.0
netstandard
System.Boolean
[System.Runtime.CompilerServices.Nullable(2)]
[<System.Runtime.CompilerServices.Nullable(2)>]
The type of elements of .
A delegate representing the method to execute.
An object containing data to be used by the method.
to prefer queueing the work item in a queue close to the current thread; to prefer queueing the work item to the thread pool's shared queue.
Queues a method specified by an delegate for execution, and specifies an object containing data to be used by the method. The method executes when a thread pool thread becomes available.
if the method is successfully queued; is thrown if the work item could not be queued.
To be added.
is .
The work item could not be queued.
mscorlib
2.0.5.0
4.0.0.0
Registers a delegate to wait for a , but does not propagate the calling stack to the worker thread.
Method
mscorlib
1.0.5000.0
2.0.0.0
2.0.5.0
4.0.0.0
netstandard
2.0.0.0
2.1.0.0
System.Threading.ThreadPool
4.1.0.0
4.1.1.0
4.1.2.0
5.0.0.0
6.0.0.0
7.0.0.0
8.0.0.0
9.0.0.0
10.0.0.0
11.0.0.0
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
[<System.Runtime.Versioning.UnsupportedOSPlatform("browser")>]
[System.Security.SecurityCritical]
[<System.Security.SecurityCritical>]
System.Threading.RegisteredWaitHandle
[System.Runtime.CompilerServices.Nullable(2)]
[<System.Runtime.CompilerServices.Nullable(2)>]
The to register. Use a other than .
The delegate to call when the parameter is signaled.
The object that is passed to the delegate.
The time-out in milliseconds. If the parameter is 0 (zero), the function tests the object's state and returns immediately. If is -1, the function's time-out interval never elapses.
to indicate that the thread will no longer wait on the parameter after the delegate has been called; to indicate that the timer is reset every time the wait operation completes until the wait is unregistered.
Registers a delegate to wait for a , using a 32-bit signed integer for the time-out in milliseconds. This method does not propagate the calling stack to the worker thread.
The object that can be used to cancel the registered wait operation.
method, does not propagate the calling stack to the worker thread. This allows code to lose the calling stack and thereby to elevate its security privileges.
> [!CAUTION]
> Using could inadvertently open up a security hole. Code access security bases its permission checks on the permissions of all the callers on the stack. When work is queued on a thread pool thread using , the stack of the thread pool thread will not have the context of the actual callers. Malicious code might be able exploit this to avoid permission checks.
Using a for `waitObject` does not provide mutual exclusion for the callbacks because the underlying Windows API uses the default `WT_EXECUTEDEFAULT` flag, so each callback is dispatched on a separate thread pool thread.
When you are finished using the that is returned by this method, call its method to release references to the wait handle. We recommend that you always call the method, even if you specify `true` for `executeOnlyOnce`. Garbage collection works more efficiently if you call the method instead of depending on the registered wait handle's finalizer.
]]>
The parameter is less than -1.
The caller does not have the required permission.
The Managed Thread Pool
Method
mscorlib
1.0.5000.0
2.0.0.0
2.0.5.0
4.0.0.0
netstandard
2.0.0.0
2.1.0.0
System.Threading.ThreadPool
4.1.0.0
4.1.1.0
4.1.2.0
5.0.0.0
6.0.0.0
7.0.0.0
8.0.0.0
9.0.0.0
10.0.0.0
11.0.0.0
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
[<System.Runtime.Versioning.UnsupportedOSPlatform("browser")>]
[System.Security.SecurityCritical]
[<System.Security.SecurityCritical>]
System.Threading.RegisteredWaitHandle
[System.Runtime.CompilerServices.Nullable(2)]
[<System.Runtime.CompilerServices.Nullable(2)>]
The to register. Use a other than .
The delegate to call when the parameter is signaled.
The object that is passed to the delegate.
The time-out in milliseconds. If the parameter is 0 (zero), the function tests the object's state and returns immediately. If is -1, the function's time-out interval never elapses.
to indicate that the thread will no longer wait on the parameter after the delegate has been called; to indicate that the timer is reset every time the wait operation completes until the wait is unregistered.
Registers a delegate to wait for a , specifying a 64-bit signed integer for the time-out in milliseconds. This method does not propagate the calling stack to the worker thread.
The object that can be used to cancel the registered wait operation.
method, does not propagate the calling stack to the worker thread. This allows code to lose the calling stack and thereby to elevate its security privileges.
> [!CAUTION]
> Using could inadvertently open up a security hole. Code access security bases its permission checks on the permissions of all the callers on the stack. When work is queued on a thread pool thread using , the stack of the thread pool thread will not have the context of the actual callers. Malicious code might be able exploit this to avoid permission checks.
Using a for `waitObject` does not provide mutual exclusion for the callbacks because the underlying Windows API uses the default `WT_EXECUTEDEFAULT` flag, so each callback is dispatched on a separate thread pool thread.
When you are finished using the that is returned by this method, call its method to release references to the wait handle. We recommend that you always call the method, even if you specify `true` for `executeOnlyOnce`. Garbage collection works more efficiently if you call the method instead of depending on the registered wait handle's finalizer.
]]>
The parameter is less than -1.
The caller does not have the required permission.
The Managed Thread Pool
Method
mscorlib
1.0.5000.0
2.0.0.0
2.0.5.0
4.0.0.0
netstandard
2.0.0.0
2.1.0.0
System.Threading.ThreadPool
4.1.0.0
4.1.1.0
4.1.2.0
5.0.0.0
6.0.0.0
7.0.0.0
8.0.0.0
9.0.0.0
10.0.0.0
11.0.0.0
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
[<System.Runtime.Versioning.UnsupportedOSPlatform("browser")>]
[System.Security.SecurityCritical]
[<System.Security.SecurityCritical>]
System.Threading.RegisteredWaitHandle
[System.Runtime.CompilerServices.Nullable(2)]
[<System.Runtime.CompilerServices.Nullable(2)>]
The to register. Use a other than .
The delegate to call when the parameter is signaled.
The object that is passed to the delegate.
The time-out represented by a . If is 0 (zero), the function tests the object's state and returns immediately. If is -1, the function's time-out interval never elapses.
to indicate that the thread will no longer wait on the parameter after the delegate has been called; to indicate that the timer is reset every time the wait operation completes until the wait is unregistered.
Registers a delegate to wait for a , specifying a value for the time-out. This method does not propagate the calling stack to the worker thread.
The object that can be used to cancel the registered wait operation.
method, does not propagate the calling stack to the worker thread. This allows code to lose the calling stack and thereby to elevate its security privileges.
> [!CAUTION]
> Using could inadvertently open up a security hole. Code access security bases its permission checks on the permissions of all the callers on the stack. When work is queued on a thread pool thread using , the stack of the thread pool thread will not have the context of the actual callers. Malicious code might be able exploit this to avoid permission checks.
Using a for `waitObject` does not provide mutual exclusion for the callbacks because the underlying Windows API uses the default `WT_EXECUTEDEFAULT` flag, so each callback is dispatched on a separate thread pool thread.
When you are finished using the that is returned by this method, call its method to release references to the wait handle. We recommend that you always call the method, even if you specify `true` for `executeOnlyOnce`. Garbage collection works more efficiently if you call the method instead of depending on the registered wait handle's finalizer.
]]>
The parameter is less than -1.
The parameter is greater than Int32.MaxValue.
The caller does not have the required permission.
The Managed Thread Pool
Method
mscorlib
1.0.5000.0
2.0.0.0
2.0.5.0
4.0.0.0
netstandard
2.0.0.0
2.1.0.0
System.Threading.ThreadPool
4.1.0.0
4.1.1.0
4.1.2.0
5.0.0.0
6.0.0.0
7.0.0.0
8.0.0.0
9.0.0.0
10.0.0.0
11.0.0.0
[System.CLSCompliant(false)]
[<System.CLSCompliant(false)>]
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
[<System.Runtime.Versioning.UnsupportedOSPlatform("browser")>]
[System.Security.SecurityCritical]
[<System.Security.SecurityCritical>]
System.Threading.RegisteredWaitHandle
[System.Runtime.CompilerServices.Nullable(2)]
[<System.Runtime.CompilerServices.Nullable(2)>]
The to register. Use a other than .
The delegate to call when the parameter is signaled.
The object that is passed to the delegate.
The time-out in milliseconds. If the parameter is 0 (zero), the function tests the object's state and returns immediately. If is -1, the function's time-out interval never elapses.
to indicate that the thread will no longer wait on the parameter after the delegate has been called; to indicate that the timer is reset every time the wait operation completes until the wait is unregistered.
Registers a delegate to wait for a , specifying a 32-bit unsigned integer for the time-out in milliseconds. This method does not propagate the calling stack to the worker thread.
The object that can be used to cancel the registered wait operation.
method, does not propagate the calling stack to the worker thread. This allows code to lose the calling stack and thereby to elevate its security privileges.
> [!CAUTION]
> Using could inadvertently open up a security hole. Code access security bases its permission checks on the permissions of all the callers on the stack. When work is queued on a thread pool thread using , the stack of the thread pool thread will not have the context of the actual callers. Malicious code might be able exploit this to avoid permission checks.
Using a for `waitObject` does not provide mutual exclusion for the callbacks because the underlying Windows API uses the default `WT_EXECUTEDEFAULT` flag, so each callback is dispatched on a separate thread pool thread.
When you are finished using the that is returned by this method, call its method to release references to the wait handle. We recommend that you always call the method, even if you specify `true` for `executeOnlyOnce`. Garbage collection works more efficiently if you call the method instead of depending on the registered wait handle's finalizer.
]]>
The caller does not have the required permission.
The Managed Thread Pool