Go Premium for a chance to win a PS4. Enter to Win

x
?
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
Medium Priority
?
516 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 300 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:Joel Coehoorn
Joel Coehoorn earned 225 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 225 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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

971 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