Killing all threads that opened by application
Solution 1
This shouldn't be happening, and if it is, you're trying to address it the wrong way.
When your application exits, the .NET Framework will automatically kill any threads whose IsBackground
property is set to "True". Designate each of your worker threads as background threads, and you won't have this problem anymore. Taking advantage of the BackgroundWorker
class and the ThreadPool
class, which automatically create background threads, is a much better option.
Otherwise, you need to clean up your foreground threads explicitly. A properly designed application will do its own bookkeeping and have a deterministic, structured way of ensuring that all its threads have been closed before exiting the Main
method. This is what you should be doing anyway if your threads require a graceful termination.
Killing the process is a very bad idea, as is letting your threads run about willy-nilly in your application.
Solution 2
You can use : Environment.Exit(0); , that will shutdown the application if threads are running and wont cause any problems.
Solution 3
You should close your threads gracefully, but just want you and others to know the way that is not recommended but possible:
on your OnClose Handler:
System.Diagnostics.Process.GetCurrentProcess().Kill();
I totally prefer Cody Gray way of doing it.
Solution 4
Well, you could call Application.Exit()
but that won't be of much help.
The bottom line is that you have to gracefully close all of the threads yourself if you want to do things properly.
Solution 5
My 2 cents... to all answers...
Try to force SHUTDOWN
Put into void CurrentApplication_Exit(object sender, ExitEventArgs e)
and private void Window_Closing(object sender, CancelEventArgs e)
those lines
System.Windows.Application.Current.ShutdownMode = ShutdownMode.OnExplicitShutdown;
System.Windows.Application.Current.Shutdown();
Night Walker
Updated on December 12, 2020Comments
-
Night Walker over 3 years
I have some really big application mixture of c# and j#.
Sometimes when I close it, there are some threads that are not closed and they are hanging in the task manager and it is impossible to kill them from there.
I have really problem to find all those threads and add them to the closing event .
Is there some way violently kill all threads that were opened by application in the closing event ?...
Thanks.
Is there maybe some Tool that can tell me what threads are opened while i close the application and who openned them ?
-
Mario over 10 yearsbut it looks better Environment.Exit(0);
-
kukido over 10 yearsThe answer will be more helpful if you add a description to the block of code.
-
Patrik almost 9 yearsWell yes this is the nice explanation.... But what if you call a WCF web service from a thread where the service is stuck in some undefined state. You can't wait endlessly. You must kill that thread. There is no other way or I am wrong ?
-
Hi-Angel over 8 yearsDidn't work. I think you meant
Environment.Exit()
like in the other answer, that works. -
Cody Gray over 8 yearsYou don't kill the thread, you call the web service with a timeout. @patrik
-
Nitin Sawant over 6 yearsthis worked along with setting
IsBackground
property of thread is set toTrue
-
Abhishek Gurjar over 5 yearsPlease provide context from link because link may expire in future.
-
Mert Akkanat about 5 yearsYes it works, especially if you use background thread and try to close all with Environment.Exit(0)
-
Hossein about 4 yearsThis is specific to wpf if I'm not mistaken
-
Alaa Sadik over 2 yearsGreat, I have a complicated thread running and found no logic to stop it before exit. Your code killed it and no more instances running in the background, Thank you