Application.SessionEnding イベント

定義

ユーザーがログオフするかオペレーティング システムをシャットダウンして、Windows セッションを終了するときに発生します。

C#
public event System.Windows.SessionEndingCancelEventHandler SessionEnding;

イベントの種類

次の例では、イベントを処理 SessionEnding し、ユーザーがイベントを取り消せるようにする方法を示します。

XAML
<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" />
C#
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;
            }
        }
    }
}

注釈

既定では、Windows セッションの終了時にアプリケーションがシャットダウンされます。これは、ユーザーがログオフまたはシャットダウンしたときに発生します。 この場合、Windows は開いている各アプリケーションにシャットダウンを求めます。 ただし、この場合、アプリケーションをシャットダウンする準備ができていない可能性があります。 たとえば、アプリケーションのデータが不整合な状態にある場合や、実行時間の長い操作の途中にある場合があります。 このような状況では、セッションの終了を防ぐことが望ましい場合があり、セッションを終了させるかどうかをユーザーが決定できるようにする方が望ましい場合があります。

イベントを処理することで、セッションがいつ終了したのを SessionEnding 検出できます。 アプリケーションでセッションの終了を防ぐ必要がある場合、SessionEndingCancelEventArgsイベント ハンドラーに渡される引数は、 にtrue設定した を公開Cancelします (既定値は falseです)。

が未処理の場合、または取り消されずに処理された場合 SessionEnding は が呼び出され、 Shutdown イベントが発生します Exit

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

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

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

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

適用対象

製品 バージョン
.NET Framework 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

こちらもご覧ください