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引数は、trueに設定したCancelを公開します (既定値はfalse)。

SessionEndingがハンドルされない場合、または取り消されずに処理された場合は、Shutdownが呼び出され、Exit イベントが発生します。

セッションが終了する理由の詳細を取得するために、アプリケーションは ReasonSessionEndingを検査できます。これは、 ReasonSessionEnding 値の 1 つです (ReasonSessionEnding.LogoffReasonSessionEnding.Shutdown)。

SessionEnding はコンソール アプリケーションによって発生しません。

SessionEnding は、 Application オブジェクトを作成するスレッドでのみ発生します。

SessionEnding は、XAML ブラウザー アプリケーション (XBAP) では発生しません。

適用対象

こちらもご覧ください