SystemEvents.SessionEnding イベント
ユーザーがシステムからログオフしようとした場合、またはシステムをシャットダウンしようとした場合に発生します。
Public Shared Event SessionEnding As SessionEndingEventHandler
[C#]
public static event SessionEndingEventHandler SessionEnding;
[C++]
public: static __event SessionEndingEventHandler* SessionEnding;
[JScript] JScript では、このクラスで定義されているイベントを処理できます。ただし、独自に定義することはできません。
イベント データ
イベント ハンドラが、このイベントに関連するデータを含む、SessionEndingEventArgs 型の引数を受け取りました。次の SessionEndingEventArgs プロパティには、このイベントの固有の情報が記載されます。
プロパティ | 説明 |
---|---|
Cancel | ユーザーによるセッションの終了要求をキャンセルするかどうかを示す値を取得または設定します。 |
Reason | セッションが終了する理由を取得します。 |
解説
このイベントはキャンセルできます。 Cancel プロパティを false に設定すると、セッションを継続して実行するように要求できます。セッションの継続を要求しても、セッションが絶対に終了しないというわけではありません。
Windows フォームでシステムのログオフまたは再起動を検出するために SessionEnding を使用している場合、このイベントの前に System.Windows.Forms.Form.Closing イベントが発生するかどうかを確実に知ることはできません。
System.Windows.Forms.Form.Closing が発生する前に何らかの特別な処理を実行するには、 System.Windows.Forms.Form.Closing の前に SessionEnding を発生させる必要があります。これを行うには、 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 fired 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 since they may cancel the shutdown
systemShutdown = False
If (DialogResult.Yes = _
MessageBox.Show("My application", "Would you care to save your work before logging off?", MessageBoxButtons.YesNo)) Then
e.Cancel = True
Else
e.Cancel = False
End If
End If
End Sub
必要条件
プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ
参照
SystemEvents クラス | SystemEvents メンバ | Microsoft.Win32 名前空間 | SessionEndingEventArgs | SessionEndingEventHandler | SessionEndReasons