Application.Exit Application.Exit Application.Exit Application.Exit Event

定义

恰好在应用程序关闭之前发生,且无法取消。Occurs just before an application shuts down, and cannot be canceled.

public:
 event System::Windows::ExitEventHandler ^ Exit;
public event System.Windows.ExitEventHandler Exit;
member this.Exit : System.Windows.ExitEventHandler 
Public Custom Event Exit As ExitEventHandler 

示例

下面的示例演示如何执行以下操作:The following example demonstrates how to:

  • Exit处理事件。Handle the Exit event.

  • 检查和更新ApplicationExitCodeExitEventArgs属性。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"
  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>
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);
                }
            }
        }
    }
}

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)
            Try
                ' Write entry to application log
                If e.ApplicationExitCode = CInt(ApplicationExitCode.Success) Then
                    WriteApplicationLogEntry("Failure", e.ApplicationExitCode)
                Else
                    WriteApplicationLogEntry("Success", e.ApplicationExitCode)
                End If
            Catch
                ' Update exit code to reflect failure to write to application log
                e.ApplicationExitCode = CInt(ApplicationExitCode.CantWriteToApplicationLog)
            End Try

            ' Persist application state
            Try
                PersistApplicationState()
            Catch
                ' 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)
                    writer.WriteLine(entry)
                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
                        writer.WriteLine(entry.Value)
                    Next entry
                End Using
            End Using
        End Sub
    End Class
End Namespace

注解

由于以下原因之一, 应用程序可能会关闭:An application can shut down for either of the following reasons:

  • Application对象Shutdown的方法以显式方式或由ShutdownMode属性确定的方式调用。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.

可以通过处理Exit事件来检测应用程序关闭发生的时间, 并根据需要执行任何其他处理。You can detect when application shutdown occurs by handling the Exit event, and perform any additional processing as required.

当不需要显式Exit调用Shutdown时, 还可以处理以检查或更改应用程序退出代码。You can also handle Exit to inspect or change the application exit code when you don't need to call Shutdown explicitly. 退出代码是从ApplicationExitCode传递给Exit事件处理程序的ExitEventArgs自变量的属性公开的。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.

如果你的应用程序SessionEnding处理该事件并随后取消Exit该事件, 则不会引发该事件, 应用程序会根据关闭模式继续运行。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.

可以从XAML 浏览器应用程序 (XBAP)XAML browser application (XBAP)设置退出代码, 但该值会被忽略。The exit code can be set from an XAML 浏览器应用程序 (XBAP)XAML browser application (XBAP), although the value is ignored.

对于XBAPXBAPs ,Exit在下列情况下将引发:For XBAPXBAPs, Exit is raised in the following circumstances:

  • XBAPXBAP离开了。An XBAPXBAP is navigated away from.

  • Internet Explorer 7Internet Explorer 7中, 当承载的XBAPXBAP选项卡关闭时。In Internet Explorer 7Internet Explorer 7, when the tab that is hosting the XBAPXBAP is closed.

  • 关闭浏览器时。When the browser is closed.

在所有情况下, 均忽略ApplicationExitCode属性的值。In all cases, the value of the ApplicationExitCode property is ignored.

适用于

另请参阅