Appliation.Exit not killing my Threads

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
 
J

John Vottero

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.
 
T

tshad

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
 
I

Ignacio Machin ( .NET/ C# MVP )

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.
 
J

Jeff Johnson

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.
 
Joined
Aug 27, 2012
Messages
1
Reaction score
0
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.
 

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

Top