Need help understanding .net ThreadPool
- by Meredith
I am trying to understand what ThreadPool does, I have this .NET example:
class Program
{
static void Main()
{
int c = 2;
// Use AutoResetEvent for thread management
AutoResetEvent[] arr = new AutoResetEvent[50];
for (int i = 0; i < arr.Length; ++i)
{
arr[i] = new AutoResetEvent(false);
}
// Set the number of minimum threads
ThreadPool.SetMinThreads(c, 4);
// Enqueue 50 work items that run the code in this delegate function
for (int i = 0; i < arr.Length; i++)
{
ThreadPool.QueueUserWorkItem(delegate(object o)
{
Thread.Sleep(100);
arr[(int)o].Set(); // Signals completion
}, i);
}
// Wait for all tasks to complete
WaitHandle.WaitAll(arr);
}
}
Does this run 50 "tasks", in groups of 2 (int c) until they all finish? Or I am not understanding what it really does.