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
504 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

751 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