Checking for open Threads when shutting down

Posted on 2007-10-05
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 85

    Accepted Solution

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

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
    Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    746 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

    15 Experts available now in Live!

    Get 1:1 Help Now