ThreadPool Class
[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]
Provides a pool of threads that can be used to post work items, process asynchronous I/O, wait on behalf of other threads, and process timers.
Assembly: mscorlib (in mscorlib.dll)
The ThreadPool type exposes the following members.
| Name | Description | |
|---|---|---|
![]() ![]() | GetMaxThreads | 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. |
![]() ![]() | GetMinThreads | Retrieves the number of idle threads the thread pool maintains in anticipation of new requests. |
![]() ![]() | QueueUserWorkItem(WaitCallback) | Queues a method for execution. The method executes when a thread pool thread becomes available. |
![]() ![]() | QueueUserWorkItem(WaitCallback, Object) | 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. |
![]() ![]() | RegisterWaitForSingleObject(WaitHandle, WaitOrTimerCallback, Object, Int32, Boolean) | Registers a delegate to wait for a WaitHandle, specifying a 32-bit signed integer for the time-out in milliseconds. |
![]() ![]() | RegisterWaitForSingleObject(WaitHandle, WaitOrTimerCallback, Object, Int64, Boolean) | Registers a delegate to wait for a WaitHandle, specifying a 64-bit signed integer for the time-out in milliseconds. |
![]() ![]() | RegisterWaitForSingleObject(WaitHandle, WaitOrTimerCallback, Object, TimeSpan, Boolean) | Registers a delegate to wait for a WaitHandle, specifying a TimeSpan value for the time-out. |
![]() ![]() | RegisterWaitForSingleObject(WaitHandle, WaitOrTimerCallback, Object, UInt32, Boolean) | Registers a delegate to wait for a WaitHandle, specifying a 32-bit unsigned integer for the time-out in milliseconds. |
![]() ![]() | SetMaxThreads | Security Critical. 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. |
![]() ![]() | SetMinThreads | Security Critical. Sets the number of idle threads the thread pool maintains in anticipation of new requests. |
The thread pool enables you to use threads more efficiently by providing your application with a pool of worker threads that are managed by the system. One thread monitors the status of several wait operations queued to the thread pool. When a wait operation completes, a worker thread from the thread pool executes the corresponding callback function.
Note: |
|---|
If you need to access user interface elements from code that is executed by a thread from the thread pool, use the System.Windows.Threading::Dispatcher class to run your code on the user interface thread. |
The threads in the managed thread pool are background threads. That is, their IsBackground properties are true.
You can also queue work items that are not related to a wait operation. To request that a work item be handled by a thread in the thread pool, call the QueueUserWorkItem 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. The thread pool has a default size of 250 worker threads per processor, and 1000 I/O completion threads.
The thread pool maintains a minimum number of idle threads. For worker threads, the default value of this minimum is the number of processors. The GetMinThreads method obtains the minimum numbers of idle worker and I/O completion threads.
When all thread pool threads have been assigned to tasks, the thread pool does not immediately begin creating new idle threads. To avoid unnecessarily allocating stack space for threads, it creates new idle threads at intervals. The interval is currently half a second, although it could change in future versions of the .NET Framework.
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 ThreadStaticAttribute attribute. Therefore, data that is placed in thread local storage by one method can be exposed to any other method that is executed by the same thread pool thread. A method that accesses a field that is marked with the ThreadStaticAttribute attribute could encounter different data depending on which thread pool thread executes it.
| Topic | Location |
|---|---|
| How to: Create an Asynchronous HTTP Handler | Building ASP .NET Web Applications |
| How to: Create an Asynchronous HTTP Handler | Building ASP .NET Web Applications |
The following example uses the QueueUserWorkItem(WaitCallback) 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.
The example displays its output in a TextBlock on the UI thread. To access the TextBlock from the callback thread, the example uses the Dispatcher property to obtain a Dispatcher object for the TextBlock, and then uses the Dispatcher::BeginInvoke method to make the cross-thread call.
Note: |
|---|
To run this example, see Building examples that have static TextBlock controls for Windows Phone 8. |


Note: