THreading close issue

I have an application that starts a thread in the main form of the project.  I have created a close button which terminstaes the thread and closes the application.  However, when the windows is closed (Xed out), the thread appears to remain active.

Is there a way I can test to see if the main form has closed, then end the thread?

Rob
robertjmackayAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

GilesBathgateCommented:
When the form closes this fires the forms Closing Event

Private Sub Form1_Closing(sender as object, e as canceleventargs) Handles Form1.Closing
   ...
   'Close thread here?
   ...
End Sub

Coudln't you just terminate the thread here?

Alternatively Can't you terminate the thread during the forms Dispose Method?
robertjmackayAuthor Commented:
Your syntax isn;t quite right ... so I changed it to below.

Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Closing
        'Close thread here?
 End Sub

however, I'm still getting this:

Method 'Form1_Closing' cannot handle Event 'Closing' because they do not have the same signature.

Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics TeacherCommented:
It should be...

    Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing

    End Sub
Become a CompTIA Certified Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

GilesBathgateCommented:
I said CancelEventArgs......

Sorry i didn't say MyBase.Closing

Prehaps I should have actually tested the code before just typing it out from memory ;)

Does this solve your problem now?
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics TeacherCommented:
On a side note, don't use the Abort() method to kill your thread.   Use a boolean flag instead that you toggle from the Closing() event.  Your Thread should check the flag and exit its main loop when the flag is toggled.  This will allow the proper cleanup of your Thread.
GilesBathgateCommented:
BTW the easy way to do it is to select the functions from the dropdowns in the IDE.

on the left dropdown above your code, the one that usually says (General) choose:

( Form1 Events )

Then from the right dropdown, choose

closing

The IDE generates the correct function header for you.
robertjmackayAuthor Commented:
>On a side note, don't use the Abort() method to kill your thread.   Use a boolean flag instead that you toggle from the >Closing() event.  Your Thread should check the flag and exit its main loop when the flag is toggled.  This will allow the >proper cleanup of your Thread.

I am not sure what you mean here
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics TeacherCommented:
Here is an example:

    Private running As Boolean
 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim t As New System.Threading.Thread(AddressOf Me.ThreadedRoutine)
        running = True
        t.Start()
    End Sub

    Private Sub ThreadedRoutine()
        While running
            Debug.WriteLine(DateTime.Now)
            System.Threading.Thread.Sleep(1000)
        Wend
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        running = False
    End Sub

The thread will continue to run forever until the "running" flag has been toggled to false.  In this case, the flag has been toggled from a Button Click() event, but you could just as easily toggle it from the Forms Closing() event for instance...

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.