Check for Finished Threads

Posted on 2009-12-17
Medium Priority
Last Modified: 2012-05-08
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

Question by:slightlyoff
  • 2
LVL 17

Expert Comment

ID: 26075821
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.
LVL 17

Expert Comment

ID: 26075828
Also you can create eventing system, like this:
LVL 86

Accepted Solution

Mike Tomlinson earned 2000 total points
ID: 26076666
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:

Author Closing Comment

ID: 31667537
Thanks!  I had to re-work my program a little bit, but it works!


Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
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…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…

862 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