Link to home
Start Free TrialLog in
Avatar of robertjmackay
robertjmackay

asked on

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
Avatar of GilesBathgate
GilesBathgate

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?
Avatar of robertjmackay

ASKER

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.

Avatar of Mike Tomlinson
It should be...

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

    End Sub
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?
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.
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.
>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
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial