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!
getHendersons()
        getMidwest()
        getGreat()
        getBand()

        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
        t.Start()
    End Sub

Open in new window

LVL 1
slightlyoffAsked:
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:
http://www.experts-exchange.com/Programming/Languages/Visual_Basic/Visual_Basic_v1.0.5.x/Q_24966480.html#26014406
0
 
CSecurityCommented:
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.
0
 
CSecurityCommented:
Also you can create eventing system, like this:
http://www.xtremevbtalk.com/showthread.php?t=159864
0
 
slightlyoffAuthor Commented:
Thanks!  I had to re-work my program a little bit, but it works!

0
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.