Need help understanding .net ThreadPool

Posted by Meredith on Stack Overflow See other posts from Stack Overflow or by Meredith
Published on 2011-02-21T23:11:22Z Indexed on 2011/02/21 23:24 UTC
Read the original article Hit count: 229

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.

© Stack Overflow or respective owner

Related posts about c#

Related posts about multithreading