36

This is slightly related to the question asked here yet the answer does not apply to my case as I am not using threads:

WPF Not closing properly

I have converted one of my WinForm application to a WPF application, nothing drastic needed to be done except for change a few words to the WPF/C# 4.0 equivalents (MessageBoxButtons to MessageBoxButton, why the one letter difference?).

Anyway, if I run the application through the debugger it runs fine until I come to close it with the "X" button to the top right of the window. The application window closes but I noticed that the debugger still shows the stop icon, checking in the Task Manager confirms it is still running.

I am not running any other threads in the background so I know it's not waiting for something else.

I've only just started with WPF but I assumed that when the user closes the application then it should just...close?

Thanks for the help!

Community
  • 1
  • 1
Jamie Keeling
  • 9,806
  • 17
  • 65
  • 102

3 Answers3

51

What is your shutdown mode? If it's explicit, then it's because you're not explicitly shutting down. If it's main window, it's because you've not assigned the main window to Application.MainWindow.

Kent Boogaart
  • 175,602
  • 35
  • 392
  • 393
  • 5
    Thanks! I never knew there were different behaviours for shutting down an application. I simply added " ShutdownMode="OnMainWindowClose" " to my application's XAML as stated in your link and it worked like a charm! – Jamie Keeling Aug 22 '10 at 22:33
16

Write this code on application closing button:

Application.Current.Shutdown()
Aziz Shaikh
  • 16,245
  • 11
  • 62
  • 79
Shivanhsu Verma
  • 161
  • 1
  • 2
  • 3
    I didn't have a closing button but by hitting the x it will call the Closed event and this worked well for me: this.Closed += (s,e) => Application.Current.Shutdown(); – Despertar May 12 '12 at 01:28
2

alternatively, add this to your App.cs

public App()
    {
        ShutdownMode = ShutdownMode.OnLastWindowClose;
    }

Detailed explanation here

Community
  • 1
  • 1