Check for Finished Threads

I run several Threads to check values in a database.  When a thread is finished it writes back to its own label on the form.  When all threads are finished, I would like to add the total of all those labels.

Not sure how to do that - how to tell when each is done.

I've attached the code for one function (one thread)... the only difference between this one and the others is the goc.GetConnection variable and the goc.theTextBox.

Thanks for your help!

        TotalOrders.Text = CInt(hOrderCount.Text) + CInt(mOrderCount.Text) + CInt(gOrderCount.Text) + CInt(pOrderCount.Text)

Public Sub getHendersons()
        lblHendersons.ForeColor = Color.White
        Dim goc As New GetOrderArgs
        goc.getConnection = HendersonConnectionString
        goc.theTextBox = Me.hOrderCount
        goc.startDelegate = AddressOf GetOrderCount
        Dim t As New Thread(AddressOf goc.StartConnection)
        threadCount += 1
        t.Name = "Thread " & threadCount.ToString()
        t.IsBackground = True
    End Sub

Open in new window

Who is Participating?
Mike TomlinsonMiddle School Assistant TeacherCommented:
One approach is to use a "controller" thread that starts all the other threads and then waits for all of them to signal a ManualResetEvent with WaitAll().

I have a simplified example here:
Create a global variable array, when each array finished, they can add a variable, like ID to that array. When you are creating a thread associate for example an ID to each and in thread code, when finished working, add ID of current thread to that global array.
Also you can create eventing system, like this:
slightlyoffAuthor Commented:
Thanks!  I had to re-work my program a little bit, but it works!

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.