Notifying main thread when all worker threads are complete

Posted on 2005-05-09
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

    One way is by the IsAlive property.



    } while (thread1.IsAlive);
    LVL 48

    Accepted Solution

    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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
    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…
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    758 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

    14 Experts available now in Live!

    Get 1:1 Help Now