.NET 2.0 Processing very large lists using ThreadPool
<p>This is further to my question <a href="http://stackoverflow.com/questions/15111178/processing-a-very-large-list-in-batches-using-system-threading-semaphore">here</a></p>

<p>By doing some reading .... I moved away from Semaphores to ThreadPool.</p>

<pre><code>using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace ThreadPoolTest
{
    class Data
    {
        public int Pos { get; set; }
        public int Num { get; set; }
    }

    class Program
    {
        static ManualResetEvent[] resetEvents = new ManualResetEvent[20];

        static void Main(string[] args)
        {            

            int s = 0;
            for (int i = 0; i &lt; 100000; i++)
            {                
                resetEvents[s] = new ManualResetEvent(false);
                Data d = new Data();
                d.Pos = s;
                d.Num = i;
                ThreadPool.QueueUserWorkItem(new WaitCallback(Process), (object)d);
                if (s &gt;= 19)
                {
                    WaitHandle.WaitAll(resetEvents);
                    Console.WriteLine("Press Enter to Move forward");
                    Console.ReadLine();
                    s = 0;
                }
                else
                {
                    s = s + 1;
                }
            }
        }

        private static void Process(object o)
        {
            Data d = (Data) o;
            Console.WriteLine(d.Num.ToString());
            Thread.Sleep(10000);
            resetEvents[d.Pos].Set();
        }
    }
}
</code></pre>

<p>This code works and I am able to process in the sets of 20. But I don't like this code because of WaitAll. So let's say I start a batch of 20, and 3 threads take longer time while 17 have finished. Even then I will keep the 17 threads as waiting because of the WaitAll.</p>

<p>WaitAny would have been good... but it seems rather messy that I will have to build so much of control structures like Stacks, Lists, Queues etc in order to use the pool efficiently.</p>

<p>The other thing I don't like is that whole global variable in the class for resetEvents. because this array has to be shared between the Process method and the main loop.</p>

<p>The above code works... but I need your help in improving it.</p>

<p>Again... I am on .NET 2.0 VS 2008. I cannot use .NET 4.0 parallel/async framework.</p>
