ThreadPool manages a group of threads. We process jobs in parallel using thread pools. The .NET Framework provides us with the System.Threading namespace, which includes the ThreadPool class. The ThreadPool type can be used on servers and in batch processing applications. ThreadPool has internal logic that makes getting a thread much less expensive
BackGroundworker thread is used Threadpool Threads that is the reason it allows update asynchronously.
Example
Console.WriteLine("Main: locking a");
object Second="";
object First="";
lock (First)
{
Console.WriteLine("Main: got First");
ThreadPool.QueueUserWorkItem(delegate {
Console.WriteLine("TPool : locking Second ");
lock (Second)
{
Console.WriteLine("TPool : got Second");
Console.WriteLine("TPool : locking First");
lock (First)
{
Console.WriteLine("TPool : got First");
}
}
}
);
Thread.Sleep(100);
Console.WriteLine("Main: locking Second");
lock (Second) {
Console.WriteLine("Main: got Second");
}
}
Console.Read();
The main methods in thread pool are
QueueUserWorkItem : Queues a method for execution. The method executes when a thread pool thread becomes available. See the above example
RegisterWaitForSingleObject : Registers a delegate that is waiting for a WaitHandle
RegisterWaitForSingleObject : Registers a delegate that is waiting for a WaitHandle
No comments:
Post a Comment