WPF application is not closing correctly

12,622

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();
}
Share:
12,622
Arsen Mkrtchyan
Author by

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, 2022

Comments

  • Arsen Mkrtchyan
    Arsen Mkrtchyan over 1 year

    I am calling Application.Current.Shutdown() from a class that is bound to xaml windows with ObjectDataProvider, 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
    Arsen Mkrtchyan almost 15 years
    No, i doesn't create threads directly, may be WPF do bindings or objectdataprovider resources in other thread?
  • Arsen Mkrtchyan
    Arsen Mkrtchyan almost 15 years
    I 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
    Mitkins almost 15 years
    surely you could replace the for(i...) with a foreach? manually iterating over loops is so last century :-)
  • Jake Berger
    Jake Berger almost 12 years
    calling Shutdown() from within override OnStartup() was NOT working. Environment.Exit(0) works