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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Forms / Threading / Timers 1
Threading question 1
Problem with Thread 1
Thread Abort and DataGrid Question 7
BackgroundWorker / Thread question 7
multi threading c# 10
Thread 1
Compact framework application loop 1

Back
Top