Introduction to ThreadPool
At the outset, let me tell what a threadpool is. Thread Pooling is an infrastructure that maintains and monitors multiple threads created by one or more applications in your system. It offers you a pool (collection) of worker threads that are managed by the system.
Most of the time, threads are in the sleeping state, as the system is idle, waiting for an event to occur. Once there is a change or update status information, they get awakened and start working what they intend to run. With the setup provided by ThreadPool, one thread monitors the status of several wait operations queued in to the thread pool. When a wait operation completes, a worker thread from the thread pool executes the corresponding callback function. Polling activates them selectively and periodically.
A thread pool is created the first time you create an instance of the
ThreadPool class. The thread pool has a default limit of 25 threads per available processor that could be changed using
CorSetMaxThreads as defined in the mscoree.h file. Each thread uses the default stack size and runs at the default priority. Each process can have only one operating system thread pool.
This class is typically used as a base class for synchronization objects.
WaitHandle is an object that controls the waiting threads. It uses a signaling mechanism to indicate taking or releasing exclusive access to a shared resource. At the same time, it also blocks the thread while waiting for access to shared resources until one or more synchronization objects receive a signal.
This class is derived from
WaitHandle and is used to notify a waiting thread that an event has occurred. It allows threads to communicate with each other by signaling.
AutoResetEvent is in the nonsignaled state, the thread blocks, waiting for the thread that currently controls the resource to signal that the resource is available by calling the
Set method. Calling
AutoResetEvent to release a waiting thread.
AutoResetEvent remains signaled until a single waiting thread is released, and then automatically returns to the nonsignaled state.
You need to supply values to the following parameters:
WaitHandle type used to register the callback method.
WaitOrTimerCallback delegate to call when the
waitObject is signaled.
- Object passed to the delegate.
- Time-out represented by a
TimeSpan. If timeout is zero, the function tests the object's state and returns immediately. If timeout is -1, the function's time-out interval never elapses.
- Boolean value "
true" to indicate the thread will no longer wait on the
waitObject parameter after the delegate has been called;
false to indicate the timer is reset every time the wait operation completes until the wait is unregistered.
RegisterWaitForSingleObject method queues up the specified delegate-method 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. You can call the "
Set" method of the
AutoResetEvent object. (Commented out in the example shown in this article).
- The time-out interval elapses.
To cancel the wait operation, you can always call the
public void show(Object O)
Console.WriteLine("Welcome to C#", "TestClass1 Object");
public void show(Object O)
Console.WriteLine("Welcome to ThreadPool", "TestClass2 Object");
static int CountDown= 20;
public static void WaitToShow(Object Obj, bool signaled)
if (Obj != null)
The first two classes defined above are used for demonstrating the
ThreadPool. The method
show defined in these two classes is the callback method to be called by the thread. The
WaitToShow method in the example class is used as a callback method during wait time. The countdown value is initialized to 20.
public static void Main()
AutoResetEvent MyEvent = new AutoResetEvent(false);
testclass t1 = new testclass();
testclass1 t2 = new testclass1();
Console.WriteLine("****Top 20 CountDown Program******");
In the above code, an instance for the wait handle,
AutoResetEvent, is created. The instance of
t1) is registered with the
WaitOrTimerCallback delegate. After queuing the work items, the thread goes into sleep mode for 10000 milliseconds. During this wait time,
WaitToShow is executed for every 500 milliseconds. I could print the countdown of numbers from 20 to 1 during this time.
Note that a call to
MyEvent.Set has been commented out.
WaitOrTimerCallback delegate is invoked at regular intervals of 500ms instead of signaling the
The output of the above program is as follows:
****Top 20 Countdown Program******
Welcome to C#
Welcome to ThreadPool
It is really amazing that a ThreadPool thread calls a method at regular intervals during wait time.