Application.Exit 事件
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
發生在應用程式關閉之前,無法取消。
public:
event System::Windows::ExitEventHandler ^ Exit;
public event System.Windows.ExitEventHandler Exit;
member this.Exit : System.Windows.ExitEventHandler
Public Custom Event Exit As ExitEventHandler
下列範例示範如何:
Exit處理事件。
檢查並更新 ApplicationExitCode 的 ExitEventArgs屬性。
將專案寫入隔離記憶體中的應用程式記錄檔。
將應用程式狀態保存到隔離的記憶體。
<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 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
應用程式可能會因為下列其中一個原因而關閉:
會 Shutdown 明確呼叫 物件的 方法 Application ,或由 屬性決定 ShutdownMode 。
用戶藉由註銷或關閉來結束會話。
您可以藉由處理 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 |