Application.Exit Olay

Tanım

Uygulama kapanmadan hemen önce gerçekleşir ve iptal edilemez.

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

Olay Türü

Örnekler

Aşağıdaki örnekte aşağıdaki adımların nasıl yapıldığını gösterilmiştir:

  • Olayı işleyebilir Exit .

  • özelliğini inceleyin ve güncelleştirin ApplicationExitCodeExitEventArgs.

  • Yalıtılmış depolamadaki uygulama günlüğüne bir giriş yazın.

  • Yalıtılmış depolama için uygulama durumunu kalıcı hale getirmek.

<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

Açıklamalar

Bir uygulama aşağıdaki nedenlerden biri nedeniyle kapatılabilir:

  • Shutdown nesnesinin Application yöntemi açıkça veya özelliği tarafından belirlendiği şekilde çağrılırShutdownMode.

  • Kullanıcı oturumu kapatarak veya kapatarak oturumu sonlandırır.

Olayı işleyerek uygulama kapatmanın ne zaman gerçekleştiğini Exit algılayabilir ve gereken ek işlemleri gerçekleştirebilirsiniz.

Açıkça çağırmanız Shutdown gerekmeyen uygulama çıkış kodunu incelemek veya değiştirmek için de işleyebilirsinizExit. Çıkış kodu, olay işleyicisine ApplicationExitCodeExit geçirilen bağımsız değişkenin ExitEventArgs özelliğinden kullanıma sunulur. Uygulama çalışmayı durdurduğunda çıkış kodu sonraki işlemler için işletim sistemine geçirilir.

Uygulamanız olayı işlerse SessionEnding ve daha sonra iptal ederse, Exit tetiklenmez ve uygulama kapatma moduna uygun olarak çalışmaya devam eder.

Çıkış kodu bir XAML tarayıcı uygulamasından (XBAP) ayarlanabilir ancak değer yoksayılır.

XBAP'ler Exit için aşağıdaki durumlarda oluşturulur:

  • Bir XBAP'tan uzaklaşılır.

  • Internet Explorer 7'de, XBAP'yi barındıran sekme kapatıldığında.

  • Tarayıcı kapatıldığında.

Her durumda, özelliğinin ApplicationExitCode değeri yoksayılır.

Şunlara uygulanır

Ayrıca bkz.