[Last Call] Learn how to a build a cloud-first strategyRegister Now


Checking for open Threads when shutting down

Posted on 2007-10-05
Medium Priority
Last Modified: 2010-04-23
Hi there

I have an application that creates lots of threads.  On shutting down I need to delay shut down until all the threads have closed.  For most of the threads thread_handler(i).IsAlive seems to work.  But the last 4 threads I use to disconnect are still alive and don't ever seem to close! As a result my code goes into a loop.

If I run call disconnection on its own outside from closing this works without problem.  and I get
The thread 0xf0c has exited with code 0 (0x0). x 4

 Its only when its in form closing - I never seem to get the above message and the threads never close.

Can anyone help me??

code is below:

Public thread_handler() As Thread 'keep track of all the threads

          Dim t As New Thread(AddressOf c.Connect) 'Declare a thread to run connect actions on the connection

    Private Sub add_to_thread_handler(ByRef t As Thread)

        Dim count As New Integer
        If thread_handler Is Nothing Then count = 0 Else count = UBound(thread_handler) + 1
        ReDim Preserve thread_handler(count)
        thread_handler(count) = t 'add thread to thread handler

    End Sub

Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing

call disconnection 'this creates 4 new threads (each thread takes about 6 seconds)

            Dim active_connections As Boolean = True
            While active_connections
                For i As Integer = 0 To UBound(thread_handler)
                    active_connections = False
                    'if there is an active connection fall out of loop
                    If thread_handler(i).IsAlive Then
                        active_connections = True
                        Exit For
                    End If
                Next i


            End While

            Log.Write_to_log(Nothing, "Application Shut down")

    End Sub
Question by:DavidGreenfield
1 Comment
LVL 86

Accepted Solution

Mike Tomlinson earned 2000 total points
ID: 20022931
By default, seperate threads do NOT allow an application to close.  If you have running threads then your app will already delay it's closing until ALL threads have closed.  In contrast, if you set the IsBackground() property of the threads to True, then they will be closed automatically when the app shuts down (this isn't what you want...just FYI).

So instead of using the polling loop in your FormClosing() event, just let the app die on it's own...it will completely exit when ALL threads have completed.

Since you have VB.Net 2005, you can call your last line of code:

    Log.Write_to_log(Nothing, "Application Shut down")

in the Application.ShutDown() event instead:
(they even show an app log being update in the sample code)

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

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
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…

829 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