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.
- Is there a safe way to check if the system is shutting down? (I tried
Environment.HasShutdownStarted, but id didnt work). - Is there a way to get notified about window state changes other than the WindowClosing event?
[Edit] The answers of the question, marked as dublicate, are not satisfying:
SystemEvents.SessionEndingis 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.SessionEndingevent) is the the best solution for my problem.