ThreadPool in C# Programming
ThreadPool is an efficient way through which we can process jobs in parallel in
C# programming language. ThreadPool is a collection of threads that can be used
to perform many tasks in background i.e. we can say that ThreadPool Provides a
pool of threads that can be used to execute tasks, post work items, process
asynchronous I/O and wait on behalf of other threads etc.
Thread pools are often employed in server applications. Each incoming request is
assigned to a thread from the thread pool, so the request can be processed
asynchronously, without tying up the primary thread or delaying the processing
of subsequent requests.
Thread pools typically have a maximum number of threads. If all the threads are
busy, additional tasks are placed in queue until they can be serviced as threads
Here I am giving an example of ThreadPool class.
Let’s see the brief demonstration of example.
// Assign notify event from ThreadPool
this.doneEvent = sendEvent;
threadIndex = (int)index;
// Call the process that is created on every thread
// Indicates that the process has been completed
// Start any process
// Start process that open c:\ directory
int totalCountProcess = 10;
// Create manualresetevent array to assign with
// Create MyProcess objects array
// Configure and launch threads using
for (int i = 0; i < totalCountProcess ; i++)
MyProcessArray[i] = p;
// ThreadPool call QueueUserWorkItem method to
execute when ThreadPool having thread
// After execute one thread it go in sleep mode
for 2 second
// Wait for all thread to complete execution