Solved

How should I know that all threads in threapool are done. I am creating threadpool threads in loop?

Posted on 2007-12-03
3
483 Views
Last Modified: 2013-12-16
I am creating threads in a loop.
for (int i = 0 ; i++, i<20)
 ThreadPool.QueueUserWorkItem(new WaitCallBack(SomeMethod), someobjectpassessomeinfoToMethod );
next

'Here I need to wait until all above threads are done with their processing

How to determine whether all threads are done.

0
Comment
Question by:scvens
3 Comments
 
LVL 10

Accepted Solution

by:
Nash2334 earned 100 total points
ID: 20397068
I have accomplished something similar using BackgroundWorkers by adding the workers to a list and then checking the IsBusy property:

int m = 1000;
private List<BackgroundWorker> bwList = new List<BackgroundWorker>();

private void StartWorkers
{
            BackgroundWorker bw;

            for (int i = 0; i < 10; i++)
            {
                bw = new BackgroundWorker();
                bw.DoWork += new DoWorkEventHandler(bw_DoWork);
                bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);
                bwList.Add(bw);
                bw.RunWorkerAsync();
            }
}

        private bool CheckWorkers()
        {
            foreach (BackgroundWorker w in bwList)
            {
                if (w.IsBusy)
                    return false;
            }

            return true;
        }
0
 
LVL 18

Assisted Solution

by:jcoehoorn
jcoehoorn earned 75 total points
ID: 20398350
WaitHandle waitHandles[20];
for (int i = 0 ; i++, i<20)
    waitHandles[i] = new AutoResetEvent(false);
    ThreadPool.QueueUserWorkItem(new WaitCallBack(SomeMethod), waitHandles[i] );
next
WaitHandle.WaitAll(waitHandles);
0
 
LVL 4

Assisted Solution

by:rafd123
rafd123 earned 75 total points
ID: 20426006
The most elegant solution to this is to use the Interlocked funtions to provide lock-free notification of when all threads are complete. The strategy involves decrementing a counter using Interlocked.Decrement within each worker method; the first worker to reach 0 can execute some completion code (e.g. fire a completion event)

Example:




using System;

using System.Threading;
 

namespace ConsoleApplication1

{

    class Program

    {

        static void Main(string[] args)

        {

            int total = 20;

            int pending = total;

            for(int i = 0; i < total; ++i)

            {

                ThreadPool.QueueUserWorkItem(delegate(object state)

                {

                    // Do some work here

                    Console.WriteLine("Hello world. My thread ID is {0}", Thread.CurrentThread.ManagedThreadId);
 

                    if(0 == Interlocked.Decrement(ref pending))

                    {

                        // Perform completion work here

                        Console.WriteLine("All Done!");

                    }

                }, null);

            }
 

            Console.ReadLine();

        }

    }

}

Open in new window

0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now