|
Thread pool is basically collection of threads which can be used for preforming
task in background. Using thread pool is a good practice over creating a new thread for you
task. Whenever the task of a thread from thread pool is completed, then the thread is
automatically returned back into the thread queue so that it can be re-used by other
asynchronous requests. .NET provides inbuilt support for thread pool. Below is a simple example
which will explain how can we use the thread pool.
using System;
using System.Threading;
static void Main()
{
// Calling MyWaitCallback asynchronously
ThreadPool.QueueUserWorkItem(MyWaitCallback, 0);
Console.WriteLine("Please wait while we are processing...");
}
public static void MyWaitCallback(object state)
{
int threadNo = (int)state;
// Processing code here
Console.WriteLine("Process Completed Thread {0}...", threadNo);
}
|