WPF application is not closing correctly
Solution 1
Have you created any threads to do background processing? If you have, make sure to set the .IsBackground
property on them, or they can keep the app running
Solution 2
Try Environment.Exit(0)
instead
Solution 3
I had a problem where the application would not shut down even when main window was closed. It turned out I had done Hide() on the splash screen instead of Close() so it was still lurking in the background keeping the application alive.
Solution 4
If you have multiple windows or dialogs in your application, you may need to close each one explicitly.
Close dialogs with:
_myDialog.Close();
Close all windows:
foreach(var window in Application.Current.Windows.ToList())
{
window.Close();
}
Arsen Mkrtchyan
Any fool can write code that a computer can understand. Good programmers write code that humans can understand. (M. Fowler)
Updated on August 02, 2022Comments
-
Arsen Mkrtchyan over 1 year
I am calling
Application.Current.Shutdown()
from a class that is bound to xaml windows withObjectDataProvider
, but the application is not closing. Can anyone help me to understand why? My application is not closing completely after my main window is closed, it doesn't disappear from task manager's process list. -
Arsen Mkrtchyan almost 15 yearsNo, i doesn't create threads directly, may be WPF do bindings or objectdataprovider resources in other thread?
-
Arsen Mkrtchyan almost 15 yearsI try this, it doesn't help, i realize where is the problem, the problem is that when i call shutdown() or close() the objectdataprovider execution is not terminated, I think it will be better to add a new question, what I will do now :)
-
Mitkins almost 15 yearssurely you could replace the for(i...) with a foreach? manually iterating over loops is so last century :-)
-
Jake Berger almost 12 yearscalling
Shutdown()
from withinoverride OnStartup()
was NOT working.Environment.Exit(0)
works