Aracılığıyla paylaş


SystemEvents.SessionEnding Olay

Tanım

Kullanıcı sistemi kapatmaya veya kapatmaya çalıştığında gerçekleşir.

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 

Olay Türü

Özel durumlar

Sistem olayı bildirimleri geçerli bağlam altında desteklenmez. Örneğin sunucu işlemleri genel sistem olay bildirimlerini desteklemeyebilir.

Sistem olayları penceresi iş parçacığı oluşturma girişimi başarılı olmadı.

Açıklamalar

Bu iptal edilebilir bir olaydır. özelliğinin Cancel olarak true ayarlanması, oturumun çalışmaya devam etme isteğinde bulunur. Oturumun bitmeyeceğini garanti etmemektedir.

Sistem oturumunu kapatma veya yeniden başlatma algılamak için bir Windows formu kullanıyorsanız SessionEnding , olayın bu olaydan önce tetiklenip tetiklenmeyeceğine karar vermenin Closing belirleyici bir yolu yoktur.

Tetiklenmeden önce Closing bazı özel görevleri gerçekleştirmek istiyorsanız, önce Closingtetiklendiğinden SessionEnding emin olmanız gerekir. Bunu yapmak için işlevini geçersiz kılarak WndProc formda yakalamanız gerekirWM_QUERYENDSESSION. Bu örnek, bunun nasıl yapılacağını gösterir.

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;
        }
    }
}

Önemli

Konsol uygulamaları olayı tetiklemiyor SessionEnding .

Uyarı

Bu olay yalnızca ileti pompası çalışıyorsa oluşturulur. Windows hizmetinde gizli bir form kullanılmadığı veya ileti pompası el ile başlatılmadığı sürece bu olay tetiklenmez. Windows hizmetinde gizli bir form kullanarak sistem olaylarının nasıl işleneceğini gösteren bir kod örneği için sınıfına SystemEvents bakın.

Dikkat

Bu statik bir olay olduğundan, uygulamanız atıldığında olay işleyicilerinizi ayırmanız gerekir; aksi takdirde bellek sızıntıları ortaya çıkar.

Şunlara uygulanır

Ayrıca bkz.