Application.Exit Ereignis
Definition
Wichtig
Einige Informationen beziehen sich auf Vorabversionen, die vor dem Release ggf. grundlegend überarbeitet werden. Microsoft übernimmt hinsichtlich der hier bereitgestellten Informationen keine Gewährleistungen, seien sie ausdrücklich oder konkludent.
Tritt direkt vor dem Herunterfahren einer Anwendung auf und kann nicht abgebrochen werden.
public:
event System::Windows::ExitEventHandler ^ Exit;
public event System.Windows.ExitEventHandler Exit;
member this.Exit : System.Windows.ExitEventHandler
Public Custom Event Exit As ExitEventHandler
Ereignistyp
Beispiele
Im folgenden Beispiel wird veranschaulicht, wie:
Behandeln Sie das Exit Ereignis.
Überprüfen und aktualisieren Sie die ApplicationExitCode Eigenschaft der ExitEventArgs.
Schreiben Sie einen Eintrag in ein Anwendungsprotokoll im isolierten Speicher.
Speichern Sie den Anwendungsstatus auf isolierten Speicher.
<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
Hinweise
Eine Anwendung kann aus einem der folgenden Gründe heruntergefahren werden:
Die Shutdown Methode des Application Objekts wird entweder explizit oder durch die ShutdownMode Eigenschaft bestimmt aufgerufen.
Der Benutzer beendet die Sitzung, indem er sich abmeldet oder heruntergefahren wird.
Sie können erkennen, wann das Herunterfahren der Anwendung auftritt, indem Sie das Exit Ereignis behandeln und bei Bedarf eine weitere Verarbeitung durchführen.
Sie können auch den Exit Anwendungsausgangscode überprüfen oder ändern, wenn Sie nicht explizit aufrufen Shutdown müssen. Der Beendigungscode wird von der ApplicationExitCode Eigenschaft des ExitEventArgs Arguments verfügbar gemacht, das an den Exit Ereignishandler übergeben wird. Wenn die Anwendung nicht mehr ausgeführt wird, wird der Beendigungscode zur nachfolgenden Verarbeitung an das Betriebssystem übergeben.
Wenn die Anwendung das SessionEnding Ereignis behandelt und anschließend abbricht, wird es nicht ausgelöst, Exit und die Anwendung wird im Einklang mit dem Herunterfahren-Modus fortgesetzt.
Der Exitcode kann aus einer XAML-Browseranwendung (XBAP) festgelegt werden, obwohl der Wert ignoriert wird.
Für XBAPs Exit wird unter den folgenden Umständen folgendes ausgelöst:
- Ein XBAP wird weg navigiert.
- Wenn die Browserregisterkarte, auf der die XBAP gehostet wird, geschlossen wird.
- Wenn der Browser geschlossen wird.
In allen Fällen wird der Wert der ApplicationExitCode Eigenschaft ignoriert.
Weitere Informationen zur XBAP-Unterstützung finden Sie unter Häufig gestellte Fragen zu von WPF browsergehosteten Anwendungen (XBAP).For more information about XBAP support, see Frequently asked questions about WPF browser-hosted applications (XBAP)