Appliation.Exit not killing my Threads

  • Thread starter Thread starter tshad
  • Start date Start date
T

tshad

I have a thread that is running on my system and I want to be able stop the
threads when I exit the program.

I have an Exit button that does:

Application.Exit()

Is there something else I have to do to kill my Threads.

It is still running an I need to go into the Task Manager to kill it.

Thanks,

Tom
 
tshad said:
I have a thread that is running on my system and I want to be able stop the
threads when I exit the program.

I have an Exit button that does:

Application.Exit()

Is there something else I have to do to kill my Threads.

It is still running an I need to go into the Task Manager to kill it.

Application.Exit() tells all message pumps to exit. It's up to you to tell
your threads that they should exit.
 
tshad said:
I normally start my threads like:

Thread oThread = new Thread(new ThreadStart(FixFields));
oThread.Start();

But I am not keeping track of the oThread. I just use it to start the
thread.

Is there a way to tell the program to just kill all my threads?

I found that System.Environment.Exit(0) seems to work and kills all my
threads.

Maybe not the best way, but it works.

Thanks,

Tom
 
Application.Exit() tells all message pumps to exit.  It's up to you to tell
your threads that they should exit.

Two suggestions

If you create tht thread using the Thread class set the IsBackground
property to true , it will prevent what you are seeing now (that a
backgroudn thread prevent the exit of the process once the UI thread
exited)
If you want to REALLY shut down your process use Environment.Exit
instead.
 
I normally start my threads like:

Thread oThread = new Thread(new ThreadStart(FixFields));
oThread.Start();

But I am not keeping track of the oThread. I just use it to start the
thread.

And there's your problem. Either you should keep track of your threads or
all your threads should periodically be checking some module-level object,
like a Boolean flag or some sort of EventWaitHandle to see if they should
exit.
 
I had the same problem with a Visual C++ managed app running on Windows XP. Had one background thread running and changed the IsBackground property of the thread to true. Then, Application::Exit() worked like a charm.
 
Back
Top