Exit Event

Application.Exit Event


Occurs just before an application shuts down, and cannot be canceled.

Namespace:   System.Windows
Assembly:  PresentationFramework (in PresentationFramework.dll)

public event ExitEventHandler Exit

An application can shut down for either of the following reasons:

  • The Shutdown method of the Application object is called, either explicitly or as determined by the ShutdownMode property.

  • The user ends the session by logging off or shutting down.

You can detect when application shutdown occurs by handling the Exit event, and perform any additional processing as required.

You can also handle Exit to inspect or change the application exit code when you don't need to call Shutdown explicitly. The exit code is exposed from the ApplicationExitCode property of the ExitEventArgs argument that's passed to the Exit event handler. When the application stops running, the exit code is passed to the operating system for subsequent processing.

If your application handles the SessionEnding event and subsequently cancels it, Exit is not raised and the application continues running in accordance with the shutdown mode.

The exit code can be set from an XAML browser application (XBAP), although the value is ignored.

For XBAPs, Exit is raised in the following circumstances:

  • An XBAP is navigated away from.

  • In Internet Explorer 7, when the tab that is hosting the XBAP is closed.

  • When the browser is closed.

In all cases, the value of the ApplicationExitCode property is ignored.

Legacy Code Example

The following example demonstrates how to:

  • Handle the Exit event.

  • Inspect and update the ApplicationExitCode property of the ExitEventArgs.

  • Write an entry to an application log in isolated storage.

  • Persist the application state to isolated storage.

Code snippet is not found. Confirm that the code snippet name 'ApplicationExitSnippets' is correct.Code snippet is not found. Confirm that the code snippet name 'ApplicationExitSnippets' is correct.

.NET Framework
Available since 3.0
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Return to top
© 2015 Microsoft