語言

Application.SessionEnding 活動

定義

當使用者登出或關閉作業系統,結束 Windows 工作階段時會發生。

public:
 event System::Windows::SessionEndingCancelEventHandler ^ SessionEnding;
public event System.Windows.SessionEndingCancelEventHandler SessionEnding;
member this.SessionEnding : System.Windows.SessionEndingCancelEventHandler 
Public Custom Event SessionEnding As SessionEndingCancelEventHandler 

事件類型

範例

以下範例示範如何處理 SessionEnding 事件並允許使用者取消。

<Application 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="SDKSample.App"
    StartupUri="MainWindow.xaml"
    SessionEnding="App_SessionEnding" />
using System.Windows;

namespace SDKSample
{
    public partial class App : Application
    {
        void App_SessionEnding(object sender, SessionEndingCancelEventArgs e)
        {
            // Ask the user if they want to allow the session to end
            string msg = string.Format("{0}. End session?", e.ReasonSessionEnding);
            MessageBoxResult result = MessageBox.Show(msg, "Session Ending", MessageBoxButton.YesNo);

            // End session, if specified
            if (result == MessageBoxResult.No)
            {
                e.Cancel = true;
            }
        }
    }
}

Imports System.Windows

Namespace SDKSample
    Partial Public Class App
        Inherits Application
        Private Sub App_SessionEnding(ByVal sender As Object, ByVal e As SessionEndingCancelEventArgs)
            ' Ask the user if they want to allow the session to end
            Dim msg As String = String.Format("{0}. End session?", e.ReasonSessionEnding)
            Dim result As MessageBoxResult = MessageBox.Show(msg, "Session Ending", MessageBoxButton.YesNo)

            ' End session, if specified
            If result = MessageBoxResult.No Then
                e.Cancel = True
            End If
        End Sub
    End Class
End Namespace

備註

預設情況下,應用程式會在 Windows 工作階段結束時關閉,這會發生在使用者登出或關機時。 當這種情況發生時,Windows 會要求每個開啟的應用程式關閉。 然而,應用程式可能尚未準備好關閉。 例如,應用程式可能有資料處於不一致狀態,或處於長期執行作業中。 在這些情況下,可能希望能阻止會話結束,甚至更希望讓使用者有權決定是否讓會話結束。

你可以透過處理 SessionEnding 事件來偵測會話何時結束。 如果應用程式需要阻止該會話結束,傳遞給事件處理器的 SessionEndingCancelEventArgs 參數會暴露 Cancel 你設定的 true 參數(預設值為 false)。

若 SessionEnding 未處理,或未被取消 Shutdown ,則呼叫並 Exit 引發事件。

為了獲得更多關於會話結束原因的資訊,應用程式可以檢查 ReasonSessionEnding,這是 ReasonSessionEnding 其中一個值 (ReasonSessionEnding.Logoff 和 ReasonSessionEnding.Shutdown)。

SessionEnding 主機應用程式不會提出。

SessionEnding 只在產生 Application 該物件的線上方升起。

SessionEnding 不為 XAML 瀏覽器應用程式(XBAP s)而被提出。

適用於

另請參閱