views:

732

answers:

0

Duplicate of

Wait for pooled threads to complete.

I have X number of functions waiting to be given to a threadpool and the threadpool has a maxnumber of threads as 5.I need to find a solution to make the main thread wait until all the X functions are executed.

Code looks like this :

              for(int i=0;i<Rulecnt;i++)
              {
                  int copy = count;
                  ds[copy] = dba.geData(ru[copy]);
                  dcont[copy] = new DataContainer(ru[copy], ds[copy], copy);
                  ThreadPool.QueueUserWorkItem(new WaitCallback(runRule[copy].performTask),dcont[count]);
              }

I want the main thread to wait until all the functions are executed.

A simple solution is to create Rulecnt no of ManualResetEvents and pass them to the threads, and wait on all the signals to arrive.

But as the max no of threads running at any time is 5 , can we just use 5 ManualResetEvents ?,If possible how?

or if there is any solution plz explain?