Thread question : Detecting application closing in a thread ?

  • Thread starter Thread starter Sagaert Johan
  • Start date Start date
S

Sagaert Johan

Hi

If i have a form application that creates a thread, is there a way to detect
from within that thread if i have closed the form ?

Now i use the forms closing event to end the thread i started, but i would
like to do it automatcally so i do not have to worry about any threads that
remain active after i closed the form.


Johan
 
The way I usually approach this is to have a static thread safe class which
contains a "still running" type flag that all spawned threads often check,
and which the main thread is able to set.

I'd recommend checking out Jon Skeet's threading pages for more
information...

http://www.yoda.arachsys.com/csharp/threads/

Thanks.
Dan.
 
Solved it :
:
In my thread i check the active form.

if (Form.ActiveForm==null)
running=false;

So i don't have to shutdown the threads explicit from my main application.
 
Ideally it would be good for the main for to have a stop method, that is
called on exiting the application, which has handles to all the threads.
This method sets the flag to false (as you have here) then waits for a
period for the threads to exit by checking their running state.
The threads are then responsible for checking the running state of the flag
after every couple of seconds, and if it is false, then stop running.
 
Back
Top