Application.Exit 이벤트

정의

애플리케이션이 종료되기 직전에 발생하며 취소할 수 없습니다.

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 이벤트, 필요에 따라 추가 처리를 수행 합니다.

처리할 수도 있습니다 Exit 검사 하거나 호출할 필요가 없는 경우 애플리케이션 종료 코드를 변경 하려면 Shutdown 명시적으로 합니다. 종료 코드는 이벤트 처리기에 전달되는 인수의 속성에서 ApplicationExitCode 노출됩니다Exit.ExitEventArgs 애플리케이션 실행을 중지, 종료 코드를 후속 처리에 대 한 운영 체제에 전달 됩니다.

애플리케이션에서 처리 하는 경우는 SessionEnding 이벤트 이후에 취소 하 고 Exit 발생 하지 않습니다 하 고 애플리케이션 종료 모드에 따라에서 실행을 계속 합니다.

값은 무시되지만 XBAP(XAML 브라우저 애플리케이션)에서 종료 코드를 설정할 수 있습니다.

XBAP Exit 의 경우 은 다음과 같은 경우에 발생합니다.

  • XBAP가 다른 위치로 이동하는 경우

  • 인터넷 Explorer 7에서 XBAP를 호스팅하는 탭이 닫힙니다.

  • 브라우저가 닫힌 경우

모든 경우에 속성 값 ApplicationExitCode 은 무시됩니다.

적용 대상

추가 정보