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
Is there a way I can test to see if the main form has closed, then end the thread?
Rob
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.
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.
It should be...
Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.Canc elEventArg s) Handles MyBase.Closing
End Sub
Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.Canc
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?
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 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.
ASKER
>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
I am not sure what you mean here
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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?