Application.Exit 事件

定義

發生在應用程式關閉之前,無法取消。

C#
public event System.Windows.ExitEventHandler Exit;

事件類型

範例

下列範例示範如何:

  • Exit處理事件。

  • 檢查並更新 ApplicationExitCodeExitEventArgs屬性。

  • 將專案寫入隔離記憶體中的應用程式記錄檔。

  • 將應用程式狀態保存到隔離的記憶體。

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);
                }
            }
        }
    }
}

備註

應用程式可能會因為下列其中一個原因而關閉:

您可以藉由處理 Exit 事件來偵測應用程式關機的時間,並視需要執行任何其他處理。

當您不需要明確呼叫Shutdown時,您也可以處理Exit以檢查或變更應用程式結束代碼。 結束代碼會從ApplicationExitCode傳遞至Exit事件處理程式之ExitEventArgs自變數的 屬性公開。 當應用程式停止執行時,結束代碼會傳遞至操作系統以供後續處理。

如果您的應用程式處理 SessionEnding 事件並後續取消事件,則不會引發, Exit 而且應用程式會根據關機模式繼續執行。

您可以從 XAML 瀏覽器應用程式設定結束代碼, (XBAP) ,不過會忽略此值。

針對 XBAP, Exit 會在下列情況下引發:

  • XBAP 會從中巡覽。
  • 當裝載 XBAP 的瀏覽器索引標籤關閉時。
  • 關閉瀏覽器時。

在所有情況下,都會忽略 屬性的值 ApplicationExitCode

如需 XBAP 支援的詳細資訊,請參閱 WPF 瀏覽器裝載應用程式的常見問題, (XBAP)

適用於

產品 版本
.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, 10

另請參閱