Application.Exit Event

Definition

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

C#
public event System.Windows.ExitEventHandler Exit;

Event Type

Examples

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.

XAML
<Application x:Class="CSharp.App"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  StartupUri="MainWindow.xaml" 
  ShutdownMode="OnExplicitShutdown"
  Exit="App_Exit"
    >
</Application>
C#
using System;
using System.Collections;
using System.Windows;
using System.IO;
using System.IO.IsolatedStorage;

namespace CSharp
{
    public enum ApplicationExitCode
    {
        Success = 0,
        Failure = 1,
        CantWriteToApplicationLog = 2,
        CantPersistApplicationState = 3
    }

    public partial class App : Application
    {
        void App_Exit(object sender, ExitEventArgs e)
        {
            try
            {
                // Write entry to application log
                if (e.ApplicationExitCode == (int)ApplicationExitCode.Success)
                {
                    WriteApplicationLogEntry("Failure", e.ApplicationExitCode);
                }
                else
                {
                    WriteApplicationLogEntry("Success", e.ApplicationExitCode);
                }
            }
            catch
            {
                // Update exit code to reflect failure to write to application log
                e.ApplicationExitCode = (int)ApplicationExitCode.CantWriteToApplicationLog;
            }

            // Persist application state
            try
            {
                PersistApplicationState();
            }
            catch
            {
                // Update exit code to reflect failure to persist application state
                e.ApplicationExitCode = (int)ApplicationExitCode.CantPersistApplicationState;
            }
        }

        void WriteApplicationLogEntry(string message, int exitCode)
        {
            // Write log entry to file in isolated storage for the user
            IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForAssembly();
            using (Stream stream = new IsolatedStorageFileStream("log.txt", FileMode.Append, FileAccess.Write, store))
            using (StreamWriter writer = new StreamWriter(stream))
            {
                string entry = string.Format("{0}: {1} - {2}", message, exitCode, DateTime.Now);
                writer.WriteLine(entry);
            }
        }

        void PersistApplicationState()
        {
            // Persist application state to file in isolated storage for the user
            IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForAssembly();
            using (Stream stream = new IsolatedStorageFileStream("state.txt", FileMode.Create, store))
            using (StreamWriter writer = new StreamWriter(stream))
            {
                foreach (DictionaryEntry entry in this.Properties)
                {
                    writer.WriteLine(entry.Value);
                }
            }
        }
    }
}

Remarks

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.
  • When the browser tab that's hosting the XBAP is closed.
  • When the browser is closed.

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

For more information about XBAP support, see Frequently asked questions about WPF browser-hosted applications (XBAP).

Applies to

Product Versions
.NET Framework 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

See also