Goal
Block or pause a reboot until my app's files have finished saving. Specifically a reboot initiated from another apps MSI installer.
Summary
The app I'm working on captures and records information during an app install on windows. Occasionally, the other app finishes installing and requires a reboot. When that happens, I need time to finish saving my files before the reboot continues.
What's Working
ShutdownBlockReasonCreate()
ShutdownBlockReasonDestory()
These work as expected; but only if I manually trigger a reboot with Start -> reboot. Windows displays the Shutdown Block message to the user with the 'force shutdown' UI (full screen blue screen)
A program is preventing shutdown.
"shutdown block reason message"
shutdown anyway?
Program Flow
- my app : begin watching install process from another app
- shutdown block reason create (message)
- other app finishes install
You must restart your system for the configuration changes to take effect.
Click 'Yes' to restart now or 'No' if you plan to restart later. - User clicks 'Yes'
- WM_QUERYENDSESSION
- return false // Do not continue reboot
- WM_ENDSESSION
- bEnding = True
- return false // App is not ready to close
ShutdownBlockReasonDestroy( )// continue shutdown
Expected Behavior
Same behavior as when I manually start a reboot. When I return false from OnQueryEndSession() the reboot should stop, or at least pause until I have destroyed the shutdown block reason. If there is a current ShutdownBlockReason, Windows should display the shutdown block reason to the user. Windows gives the user the chance to shutdown now, wait for the process to finish, or wait and go back to save their work.
Current Behavior
- The Windows Force Shutdown UI is never displayed to the user.
- The BlockShutdownReason is never displayed to the user.
- The app is not given time to complete the file save.
Windows asks if the shutdown can continue (WM_QUERYENDSESSION) but always follows that up with WM_ENDSESSION, bEnding = True. Windows gives minimal time to complete the file save (3 seconds) and does not give my app a chance to inform the user that a file save is in progress.
The user is not informed that a file save is in progress.
What I've Tried
- Create and destroy ShutdownBlockReason
- CreateProcess("C:\...\shutdown.exe /a")
- AbortSystemShutdown
- if I call this before the shutdown has started:
'Error: No shutdown is in progress'
- if I call this after the shutdown has begun:
'Error: A shutdown is in progress'
- que frustrated confused developer
- Set registry key:values to not automatically shut down apps
- AutoEndTasks : False
- HungAppTimeout : 120_000
- WaitToKillAppTimeout : 120_000
- Validation
- I have ensured the application has valid shutdown permissions
- I have set the permissions with the app token
- I have double checked that the permissions were set correctly
- I have used
ShutdownBlockReasonQuery()to validate that the shutdown block reason exists before during and after OnQueryEndSession and OnEndSession. - I have validated that the window is a valid top level window.
References
Windows Official Documentation
- ShutdownBlockReasonCreate
- ShutdownBlockReasonDestroy
- Application Shutdown Changes in Windows Vista
- Shutting Down
- Example :: How to shut down the system
- Force Reboot
- Disable Automatic Application Shutdown
- Enables the files in use dialog (prior to install)
- Does not effect the Force Shutdown (blue screen) UI (at the end of the install)
- Restart Dialog Function
- ExitWindowsEx
- Windows Installer : System Reboots
Blogs and Forums
- Now that windows makes it harder for your problem to block shutdown, how do you block shutdown?
- Stack Overflow :: Example of ShutdownBlockReasonCreate
- Stack Overflow :: ShutdownBlockReasonCreate causes shutdown to be canceled after one minute
- Turn off automatic termination of applications
- Allows non-window applications to be considered valid when responding to "WM_QUERYENDSESSION"
- My application has a top level window and should already be considered valid, as evidenced when the user clicks Start -> Reboot
- SO : Is there a way to delay a forced reboot
- Only applies to Windows Update reboots
- Suppress a MSI Reboot
- Suppress reboot from the originating installer, not a 3rd party application.
- Four ways to stop a shutdown or reboot
- These apply to normal reboots, not a reboot from Windows Installer, which overrides all options.
None of these solutions have provided a way to block a shutdown from the end of an install, when it asks if you want to shutdown.
They all work as expected when the user clicks Start -> shutdown, but otherwise all attempts I have made to block or delay the shutdown have failed with no message to the user, and no effect on the shutdown / reboot.