In my WPF application I'm using SessionEnding Event to prevent from shutting Windows down when my application needs to save data. It works, but I don't know how to tell Windows I don't need to block its shutdown anymore. I have a bool variable that tells whether the saving is happening right now. If I block shutdown in SessionEnding, how to close my app when it ends? I don't want to simply close app when saving ends cuz I don't know if user has canceled shutdown. Please help.
Asked
Active
Viewed 554 times
0
-
2How long does your data saving take? If it's quick, you can just save in response, and then allow shutdown to continue. Otherwise, if you block shutdown, the user will need to manually shutdown again. – dlev Feb 27 '12 at 20:41
1 Answers
1
When shutting down Windows only waits for your application to close. Once your application saves the data be sure to close it, Windows should detect that an continue shutting down.

NestorArturo
- 2,476
- 1
- 18
- 21
-
Ok, but what if I close my app after saving, but user has canceled shutdown? I want my App not to close then. How can I be informed about canceling shutdown while user waits for my App to close. – jozefkarton Feb 27 '12 at 21:03
-
Not tested, but check for this property: Environment.HasShutdownStarted – NestorArturo Feb 27 '12 at 21:39
-
@jozefkarton I don't think there is a pure WPF way of detecting that situation. You might be able to get something out of overriding WndProc and handling WM_QUERYENDSESSION and WM_ENDSESSION in a tricky way. My advice is that you don't really need to recover from the scenario where the user has pressed "cancel shutdown" while your app is shutting down due to a system shutdown. I don't think users will be surprised when a few applications (yours included) are closed during shutdown, even though they pressed cancel at some point. – Mike Clark Feb 27 '12 at 21:54