Notifying main thread when all worker threads are complete

Posted on 2005-05-09
Medium Priority
Last Modified: 2008-01-09
My main thread spawns a number of worker threads to process records in a file. The number of threads is controlled by the user, based on his or her available hardware, and the number of records in the file is unknown.

I need to know when *all* my worker threads are complete, so I can then close the file and perform other finalise processing. I'm creating my own threads rather than using the threadpool, as I figured this would be more appropriate, but maybe that's wrong?

The basic structure of my code so far is listed below. What I need to know is what would be the best way to check that all worker threads have completed, and where I need to check that. I've tried keeping a counter of the threads as they start and finish processing, but that suffered from race conditions. I'm assuming I can't Join the worker threads, as the moment I join the first one, I won't be able to join any others until the first one ends. Basically, I'm stuck :)

Many thanks

public void Process()
      private int m_cpuThreads;

      for (int t = 0; t < m_cpuThreads; t++)
            ThreadStart threadStart = new ThreadStart(this.ProcessRecords);
            Thread workerThread = new Thread(threadStart);

private void ProcessRecords()
            while (true)
                  ThreadControl threadCtrl = ReadFile();  //Threadsafe method to read records from file and check if EOF reached.

                  if (threadCtrl.RecordsRead == 0)
                        //No more records left to process, so quit loop
                  ....code to process file records
      catch (System.Exception e)
Question by:gamesmeister
LVL 12

Expert Comment

ID: 13958981
One way is by the IsAlive property.



} while (thread1.IsAlive);
LVL 48

Accepted Solution

AlexFM earned 2000 total points
ID: 13959022
You can assosiate ManualResetEvent instance with every worker thread. Thread signals it's event before exit. To wait for all threads use WaitHandle.WaitAll Method passing array of these events to it.

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This video tutorial shows you the steps to go through to set up what I believe to be the best email app on the android platform to read Exchange mail.  Get the app on your phone: The first step is to make sure you have the Samsung Email app on your …
Stellar Phoenix SQL Database Repair software easily fixes the suspect mode issue of SQL Server database. It is a simple process to bring the database from suspect mode to normal mode. Check out the video and fix the SQL database suspect mode problem.

609 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