Application.SessionEnding Event

Occurs when the user ends the Windows session by logging off or shutting down the operating system.

Namespace:  System.Windows
Assembly:  PresentationFramework (in PresentationFramework.dll)
XMLNS for XAML: http://schemas.microsoft.com/winfx/2006/xaml/presentation, http://schemas.microsoft.com/netfx/2007/xaml/presentation

member SessionEnding : IEvent<SessionEndingCancelEventHandler,
    SessionEndingCancelEventArgs>
<object SessionEnding="SessionEndingCancelEventHandler" .../>

By default, an application shuts down when the Windows session ends, which occurs when a user logs off or shuts down. When this happens, Windows asks each open application to shut down. However, it is possible that an application may not be ready to shut down when this occurs. For example, an application may have data that is in an inconsistent state, or in the middle of a long-running operation. In these situations, it may be desirable to prevent the session from ending, and may be more desirable to allow users the option to decide whether or not to let the session to end.

You can detect when a session ends by handling the SessionEnding event. If an application needs to prevent the session from ending, the SessionEndingCancelEventArgs argument that is passed to the event handler exposes the Cancel that you set to true (the default value is false).

If SessionEnding is unhandled, or is handled without being cancelled, Shutdown is called and the Exit event is raised.

To obtain more information about why the session is ending, an application can inspect ReasonSessionEnding, which is one of the ReasonSessionEnding values (Logoff and Shutdown).

SessionEnding is not raised by console applications.

SessionEnding is raised only on the thread that creates the Application object.

SessionEnding is not raised for XAML browser applications (XBAPs).

The following example demonstrates how to handle the SessionEnding event and allow the user to cancel it.

<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" />
No code example is currently available or this language may not be supported.

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft