Paul Matovich
Paul Matovich

Reputation: 1516

Application.Current.MainWindow.Close vs Application.Current.Shutdown

Should Application.Current.Shutdown() NOT be used as a best practice to close a WPF application?

I have a small, multiple window application and the "shutdown" seems to work nicely for all my exit commands. However, I have been told to always use Application.Current.MainWindow.Close().

Upvotes: 8

Views: 4119

Answers (1)

brunnerh
brunnerh

Reputation: 185578

MainWindow.Close won't even do anything if the ShutdownMode is not set respectively. You can do what you want, it should just fit what one expects.

(Shutdown will ignore the fact that some Closing event was cancelled, so that would be one thing you need to take into consideration)

Also:

Shutdown is implicitly called by Windows Presentation Foundation (WPF) in the following situations:

  • When ShutdownMode is set to OnLastWindowClose.

  • When the ShutdownMode is set to OnMainWindowClose.

  • When a user ends a session and the SessionEnding event is either unhandled, or handled without cancellation.

Upvotes: 7

Related Questions