This documentation is archived and is not being maintained.

Application.Exit Event

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

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

Public Event Exit As ExitEventHandler
<object Exit="ExitEventHandler" .../>

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.

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.

<Application x:Class="CSharp.App"

Imports Microsoft.VisualBasic
Imports System
Imports System.Collections
Imports System.Windows
Imports System.IO
Imports System.IO.IsolatedStorage

Namespace VisualBasic
    Public Enum ApplicationExitCode
        Success = 0
        Failure = 1
        CantWriteToApplicationLog = 2
        CantPersistApplicationState = 3
    End Enum

    Partial Public Class App
        Inherits Application
        Private Sub App_Exit(ByVal sender As Object, ByVal e As ExitEventArgs)
                ' Write entry to application log
                If e.ApplicationExitCode = CInt(ApplicationExitCode.Success) Then
                    WriteApplicationLogEntry("Failure", e.ApplicationExitCode)
                    WriteApplicationLogEntry("Success", e.ApplicationExitCode)
                End If
                ' Update exit code to reflect failure to write to application log
                e.ApplicationExitCode = CInt(ApplicationExitCode.CantWriteToApplicationLog)
            End Try

            ' Persist application state
                ' Update exit code to reflect failure to persist application state
                e.ApplicationExitCode = CInt(ApplicationExitCode.CantPersistApplicationState)
            End Try
        End Sub

        Private Sub WriteApplicationLogEntry(ByVal message As String, ByVal exitCode As Integer)
            ' Write log entry to file in isolated storage for the user
            Dim store As IsolatedStorageFile = IsolatedStorageFile.GetUserStoreForAssembly()
            Using stream As Stream = New IsolatedStorageFileStream("log.txt", FileMode.Append, FileAccess.Write, store)
                Using writer As New StreamWriter(stream)
                    Dim entry As String = String.Format("{0}: {1} - {2}", message, exitCode, Date.Now)
                End Using
            End Using
        End Sub

        Private Sub PersistApplicationState()
            ' Persist application state to file in isolated storage for the user
            Dim store As IsolatedStorageFile = IsolatedStorageFile.GetUserStoreForAssembly()
            Using stream As Stream = New IsolatedStorageFileStream("state.txt", FileMode.Create, store)
                Using writer As New StreamWriter(stream)
                    For Each entry As DictionaryEntry In Me.Properties
                    Next entry
                End Using
            End Using
        End Sub
    End Class
End Namespace

.NET Framework

Supported in: 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.