Occurs when the user is trying to log off or shutdown the system.
[Visual Basic] Public Shared Event SessionEnding As SessionEndingEventHandler [C#] public static event SessionEndingEventHandler SessionEnding; [C++] public: static __event SessionEndingEventHandler* SessionEnding;
[JScript] In JScript, you can handle the events defined by a class, but you cannot define your own.
The event handler receives an argument of type SessionEndingEventArgs containing data related to this event. The following SessionEndingEventArgs properties provide information specific to this event.
|Cancel||Gets or sets a value indicating whether to cancel the user request to end the session.|
|Reason||Gets the reason the session is ending.|
This is a cancellable event. Setting the Cancel property to false will request that the session continues to run. It provides no guarantee that the session will not end.
If you are using SessionEnding in a Windows form to detect a system logoff or reboot, there is no deterministic way to decide whether the System.Windows.Forms.Form.Closing event will fire before this event.
If you want to perform some special tasks before System.Windows.Forms.Form.Closing is fired, you need to ensure that SessionEnding fires before System.Windows.Forms.Form.Closing. To do this, you need to trap the WM_QUERYENDSESSION in the form by overriding the WndProc function. The following example demonstrates how to do this in a deterministic way,
[Visual Basic] 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
Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family