May 20, 2009 Leave a comment
Withing WPF application, you can detect if the user is shutting down or logging off and thereby take some action like saving for example your project. In order to do so, you will need to register to the Application SessionEnding event.
Application.Current.SessionEnding +=new SessionEndingCancelEventHandler(Current_SessionEnding);
You can then use the SessionEndingCancelEventArgs.ReasonSessionEnding to know exactly the reason of the session ending, whether it was because of a shuttdown or logoff.
private void Current_SessionEnding(object sender, SessionEndingCancelEventArgs e)
if (e.ReasonSessionEnding == ReasonSessionEnding.Logoff)
MessageBox.Show("User Logged off the computer");
else if (e.ReasonSessionEnding == ReasonSessionEnding.Shutdown)
MessageBox.Show("User Shut down his machine");