0

My application on startup may start a few thread concurrently. Now based on some condition I would like to completely kill off all the thread regardless the state of other threads.

I have tried App.Current.ShutDown() as well as Application.Current.ShutDown but doesn't work?

1
  • ..."irregardless" is not a word. Couldn't help myself. As to your problem, you need to show us your code. In the most simple case where you are creating new Threads, set the IsBackground property to true if you want them to share the lifetime of your application. Commented Mar 27, 2014 at 3:01

4 Answers 4

4

You can try

Environment.Exit(0); 

You can replace 0 with any code you want from here

You should see Killing all threads that opened by application (and Shutting down a multithreaded application consequently) as I think he provides some solid advice.

Sign up to request clarification or add additional context in comments.

3 Comments

You could also do (not recommended) stackoverflow.com/questions/5901679/…
I'm not sure if there's any downside of it but it achieve my objective
@Mr.SuicideSheep I just think that it is less "clean", but that is just my opinion
3

A thread is either a background thread or a foreground thread. Background threads are identical to foreground threads, except that background threads do not prevent a process from terminating. Once all foreground threads belonging to a process have terminated, the common language runtime ends the process. Any remaining background threads are stopped and do not complete.

Set your thread's property IsBackground=true

var t= new Thread(); t.IsBackground = true; 

Also See this: How to: Create and Terminate Threads (C# Programming Guide)

Comments

3

If you need to kill the running application, regardless of state you can either use

Environment.Exit(0); // use -1 if you're exiting with an error, exiting with 0 is considered to have exited without errors. 

Or if you really want to use the hammer

Environment.FailFast() 

FailFast's documentation says: Immediately terminates a process after writing a message to the Windows Application event log, and then includes the message in error reporting to Microsoft.

Use the FailFast method instead of the Exit method to terminate your application if the state of your application is damaged beyond repair, and executing your application's try/finally blocks and finalizers will corrupt program resources.

Comments

2

If your other threads are background threads they will end (i.e. abort silently when you shutdown the WPF application which is running on the only foreground thread):

From MSDN:

"...background threads do not prevent a process from terminating. Once all foreground threads belonging to a process have terminated, the common language runtime ends the process. Any remaining background threads are stopped and do not complete."

e.g.

Thread myThread = new Thread(); myThread.IsBackground = true; 

ThreadPool threads background ones.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.