[RESOLVED] [2005] Closing threads when program closes
Hi Everyone,
I have an application that has threads continually running in the background monitoring different settings. When the end users x's out of my program, the threads remain as a process in windows even though the GUI is closed.
How do I terminate the threads when the GUI is x'd out, or how do I put logic into the threads to end if the GUI is closed?
Thank you all in advance.
Re: [2005] Closing threads when program closes
like this, i think thats the right name for that event...maybe before close or something, im sure youl get it.....
Code:
private sub on_close(byval sender as object... blabla) handles me.onclosing
yourthread.stop
end sub
Re: [2005] Closing threads when program closes
vb Code:
Private Sub form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
I use the form closing event to deal with things like this. If your using background workers to run the other threads then couldnt you use backgroundworkername.dispose() ?
Re: [2005] Closing threads when program closes
Set the IsBackground property of each Thread object to True. That is only safe to do if the threads are guaranteed not to leave anything in an invalid state if they are terminated implicitly.
Re: [2005] Closing threads when program closes
On the _FormClosed event, how would I stop a thread that is started like the below code?
Code:
Dim t As New Thread(AddressOf runContactThread)
t.Start()
JMC,
Quote:
Set the IsBackground property of each Thread object to True.
How would I do that with the above code?
Re: [2005] Closing threads when program closes
Does anyone know how to kill this thread on the _FormClosed event?
Code:
Dim t As New Thread(AddressOf runContactThread)
t.Start()
Re: [2005] Closing threads when program closes
Quote:
Originally Posted by Giraffe Frenzy
Does anyone know how to kill this thread on the _FormClosed event?
Code:
Dim t As New Thread(AddressOf runContactThread)
t.Start()
Just use the IsBackground method like jmcilhinney mentioned.
Code:
Dim t As New Thread(AddressOf runContactThread)
t.IsBackGround = True
t.Start()
Re: [2005] Closing threads when program closes
I figured it out.
On the _FormClosed event, type
Re: [2005] Closing threads when program closes
Quote:
Originally Posted by Giraffe Frenzy
I figured it out.
On the _FormClosed event, type
You shouldnt be using End. End is an old vb6 way of terminating the application. Why would you want to do that in the formclosed event?
Did you even try the IsBackground property?
Re: [RESOLVED] [2005] Closing threads when program closes
jmc and Atheist,
thank you. I tried IsBackground as you suggested. This also worked. Based on what you said Atheist, I will use the IsBackground. Thank you all.