Hopefully an easy question,
I have a thread running in a class that just loops through a bunch of things. I want to stop the thread when the user Closes out of the form. I have the following function in my Form.vb
Public Function getFormStatus() As Boolean
Dim bAlive As Boolean = False
If Me.Enabled = True Then
bAlive = True
Else
bAlive = False
End If
Return bAlive
End Function
Then In my main loop class I have
dim isAlive as Boolean = false
isAlive = frm.getFormStatus
while isAlive
do stuff
My problem is I don’t know the property of the Form that tells me whether or not it is currently Active! I feel like this shouldn’t be difficult but I’ve tried several different ones and none have given my the expected result.
A thread is active when it’s not completed running what it has been told to run. While that may sound silly, in your above example, you will note that as long as you are in that while loop, the thread is active.
If you are looking to perform execution on a seperate thread form the UI thread, I would suggest looking at the BackgroundWorker as it will make your approach much cleaner and easier to implement.
With regards to the active form, that can be achieved via the ActiveForm property.