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
498 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

The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

808 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