SystemEvents.SessionEnding 이벤트

정의

사용자가 로그오프하려고 하거나 시스템을 종료하려고 할 때 발생됩니다.

public:
 static event Microsoft::Win32::SessionEndingEventHandler ^ SessionEnding;
public static event Microsoft.Win32.SessionEndingEventHandler SessionEnding;
public static event Microsoft.Win32.SessionEndingEventHandler? SessionEnding;
member this.SessionEnding : Microsoft.Win32.SessionEndingEventHandler 
Public Shared Custom Event SessionEnding As SessionEndingEventHandler 

이벤트 유형

예외

현재 컨텍스트에서는 시스템 이벤트 알림이 지원되지 않습니다. 예를 들어, 서버 프로세스에서 전역 시스템 이벤트 알림을 지원하지 않을 수 있습니다.

시스템 이벤트 창 스레드를 만들지 못한 경우

설명

취소할 수 있는 이벤트입니다. 속성을 trueCancel 설정하면 세션이 계속 실행되도록 요청합니다. 세션이 종료되지 않을 것이라는 보장은 없습니다.

Windows 양식에서 를 사용하여 SessionEnding 시스템 로그오프 또는 다시 부팅을 검색하는 경우 이 이벤트 전에 이벤트가 발생할지 여부를 Closing 결정하는 결정적인 방법은 없습니다.

가 실행되기 전에 Closing 몇 가지 특수 작업을 수행하려는 경우 이전에 Closing가 실행되는지 SessionEnding 확인해야 합니다. 이렇게 하려면 함수를 재정의하여 폼에서 를 트래핑 WM_QUERYENDSESSIONWndProc 해야 합니다. 이 예제에서는 이 작업을 수행하는 방법을 보여 줍니다.

Private Shared WM_QUERYENDSESSION As Integer = &H11  
 Private Shared systemShutdown As Boolean = False  
 Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)  
     If m.Msg = WM_QUERYENDSESSION Then  
         MessageBox.Show("queryendsession: this is a logoff, shutdown, or reboot")  
         systemShutdown = True  
     End If  
     ' If this is WM_QUERYENDSESSION, the closing event should be raised in the base WndProc.  
     MyBase.WndProc(m)  
 End Sub 'WndProc   
 Private Sub Form1_Closing(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing  
     If (systemShutdown) Then  
     ' Reset the variable because the user might cancel the shutdown.  
         systemShutdown = False  
         If (System.Windows.Forms.DialogResult.Yes = _  
                 MessageBox.Show("My application", "Do you want to save your work before logging off?", MessageBoxButtons.YesNo)) Then  
                 e.Cancel = True  
         Else  
                 e.Cancel = False  
         End If  
     End If  
 End Sub  
private static int WM_QUERYENDSESSION = 0x11;  
private static bool systemShutdown = false;  
protected override void WndProc(ref System.Windows.Forms.Message m)  
{  
    if (m.Msg==WM_QUERYENDSESSION)  
    {  
        MessageBox.Show("queryendsession: this is a logoff, shutdown, or reboot");  
        systemShutdown = true;  
    }  

    // If this is WM_QUERYENDSESSION, the closing event should be  
    // raised in the base WndProc.  
    base.WndProc(ref m);  

} //WndProc   

private void Form1_Closing(  
    System.Object sender,   
    System.ComponentModel.CancelEventArgs e)  
{  
    if (systemShutdown)  
        // Reset the variable because the user might cancel the   
        // shutdown.  
    {  
        systemShutdown = false;  
        if (DialogResult.Yes==MessageBox.Show("My application",   
            "Do you want to save your work before logging off?",   
            MessageBoxButtons.YesNo))  
        {  
            e.Cancel = true;  
        }  
        else  
        {  
            e.Cancel = false;  
        }  
    }  
}  

중요

콘솔 애플리케이션 발생 하지 않습니다는 SessionEnding 이벤트입니다.

참고

이 이벤트는 메시지 펌프가 실행 중인 경우에만 발생합니다. Windows 서비스에서 숨겨진 양식을 사용하거나 메시지 펌프를 수동으로 시작하지 않으면 이 이벤트가 발생하지 않습니다. Windows 서비스에서 숨겨진 양식을 사용하여 시스템 이벤트를 처리하는 방법을 보여 주는 코드 예제는 클래스를 SystemEvents 참조하세요.

주의

정적 이벤트 이기 때문에 애플리케이션 삭제 되거나 메모리 누수가 발생 하는 경우 이벤트 처리기를 분리 해야 합니다.

적용 대상

추가 정보