Reputation: 75
I have a WPF application that needs to save its current state every time the application exits. At the moment, I am handling the System.Windows.Application.Exit
event to save the state of the application. However, it seems that the event is not invoked system reboots -- only ordinary shutdowns. Is this expected behavior?
This is what my application looks like.
public class MyApp : Application
{
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
...
this.Exit += OnExit;
}
private void OnExit(object sender, ExitEventArgs e)
{
SaveMyApplicationState();
}
}
What events can I use to be notified of a system reboot, so my application state can be saved?
Upvotes: 0
Views: 418
Reputation: 8935
To determine when system is restarting or shutting down use the SessionEnding
event:
App.xaml
<Application ...
SessionEnding="App_SessionEnding">
...
</Application>
App.xaml.cs
public partial class App : Application
{
void App_SessionEnding(object sender, SessionEndingCancelEventArgs e)
{
if (.../*canceling*/)
{
e.Cancel = true;
}
else
{
// Processing before terminating (save current state)
// ...
}
}
}
For detailed information see documentation: Application.SessionEnding Event
NOTE: Application Shutdown Changes in Windows Vista
Upvotes: 1