I am working on a WPF application that should store its window state (normal / minimized) - even if the system is shutting down or loggin off.
The WindowClosing event is used to store the state. The problem is, that shutdown/log off enforces the window to close. Therefore, the window state is always minimized after reboot.
Environment.HasShutdownStarted
, but id didnt work).[Edit] The answers of the question, marked as dublicate, are not satisfying:
SystemEvents.SessionEnding
is not a proper solution because the msdn says: "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 Closing event will fire before this event."
GetSystemMetrics(SM_SHUTTINGDOWN)
may work (i didn't test it), but it requires PInvoke and I prefer a managed solution.
So finally, the suggestion from d.moncada (using the Application.SessionEnding
event) is the the best solution for my problem.
Is there a safe way to check if the system is shutting down? (I tried Environment.HasShutdownStarted, but id didnt work).
Do you want to know when the system (computer) is shutting down, or the application?
If it's the application, you can use Window.Closing
event.
See here.
If it's for the system, you can listen to the Application.SessionEnding
event.
See here.
Is there a way to get notified about window state changes other than the WindowClosing event?
Have you taken a look at the StateChanged
event of the Window
?
More info, see here