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 

事件類型

例外狀況

目前內容不支援系統事件告知。 例如,伺服器處理序可能就不支援全域系統事件告知。

建立系統事件視窗執行緒的嘗試未成功。

備註

這是可取消的事件。 將 Cancel 屬性設定為 true 會要求工作階段繼續執行。 它不保證會話不會結束。

如果您是在 Windows 窗體中使用 SessionEnding 來偵測系統註銷或重新啟動,則沒有任何決定性的方式可以決定是否 Closing 要在此事件之前引發事件。

如果您想要在引發之前 Closing 執行一些特殊工作,您必須確保 SessionEnding 會在 之前 Closing引發。 若要這樣做,您必須覆WndProc寫 函式,以在表單中設陷 WM_QUERYENDSESSION 。 此範例示範如何執行這項操作。

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

警告

因為這是靜態事件,所以您必須在處置應用程式時卸離事件處理程式,否則記憶體流失將會產生。

適用於

另請參閱