Share via


Windows.System.Threading Namespace

Enables an application to use the thread pool to run work items.

The thread pool provides a pool of worker threads that an application can use for work items that can run in parallel. Using the thread pool is simpler and more efficient than creating individual threads, because the thread pool manages thread lifetimes and schedules work items when threads become available.

Work items run asynchronously and can run in any order, so it's best to use the thread pool for work items that don't take a lot of time and don't depend on each other's output. You create a work item by calling the RunAsync method. The work item is added to the thread pool's queue, where it stays until a thread becomes available. When a thread becomes available, the thread pool calls the work item's associated WorkItemHandler delegate and the work item runs.

The thread pool schedules a work item as soon as a thread becomes available, so the item might run immediately. If you want the work item to run after a certain amount of time, use a timer. You create a timer and specify its timeout value by calling the CreateTimer method. The timer begins counting down as soon as it is created, so you don't have to start it. When the timer expires, the thread pool calls the timer's TimerElapsedHandler delegate.

If you want to run a work item more than once at a regular interval, use the CreatePeriodicTimer method to create a periodic timer. A periodic timer is reset every time it expires and stays active until you cancel it.

If you want a work item to wait until an event or semaphore is signaled, or if you want to create a preallocated work item, see Windows.System.Threading.Core.

For example code that demonstrates how to use the thread pool, see the Thread Pool Sample.

Note

The ThreadPool API is supported for desktop as well as UWP apps.

Classes

ThreadPool

Provides access to the thread pool. See Threading and async programming for detailed guidance on using the thread pool:

Note

The ThreadPool API is supported for desktop as well as UWP apps.

ThreadPoolTimer

Represents a timer created with CreateTimer or CreatePeriodicTimer.

Note

The ThreadPool API is supported for desktop as well as UWP apps.

Enums

WorkItemOptions

Specifies how work items should be run.

WorkItemPriority

Specifies the priority of a work item relative to other work items in the thread pool.

Delegates

TimerDestroyedHandler

Represents a method that is called when a timer created with CreateTimer or CreatePeriodicTimer is complete.

TimerElapsedHandler

Represents a method that is called when a timer created with CreateTimer or CreatePeriodicTimer expires.

WorkItemHandler

Represents a method that is called when a work item runs.