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?
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.