Page 1 of 1

Manage multiple programs which want to shut down Windows

Posted: Mon Nov 27, 2023 4:58 pm
by jacdelad
I have the folling problem/situation: My Windows computer runs two programs simultaniously, a backup program and a video converter. Now it shall shut down after all is done. Both programs offer an option to shut down the computer automatically, but of course, the second program would still be running.
Now I know, I could write a program myself which monitors if certain programs are still running (I can tell the to simply close instead of shutting down after doing their stuff).
But, does Windows offer an API for that, like RegisterForShutDown_(), which simply waits until all registered programs are done.
I know this API wouldn't solve my problem with these external programs, I'm just curious.

Re: Manage multiple programs which want to shut down Windows

Posted: Mon Nov 27, 2023 8:19 pm
by ChrisR
This doesn't really answer the question, but if needed, to avoid auto ShutDown, you can use Sysinternals PsSuspend
To suspend the shutdown:

Code: Select all

pssuspend64.exe winlogon.exe
pssuspend64.exe wininit.exe
To resume (it will shutdown if a request has been made previously):

Code: Select all

pssuspend64.exe -r winlogon.exe
pssuspend64.exe -r wininit.exe

Re: Manage multiple programs which want to shut down Windows

Posted: Tue Nov 28, 2023 11:46 am
by spikey
I'm not aware of an API function that will do this in this particular way. You could:

1) If the programs will call a script, create a "lock" file when the tasks begin: "%temp%\lock1.txt" and "%temp%\lock2.txt". Create two batch files which erase their own lock file and look for both "lock" files and run these at the end of the tasks. One of them should exit with a shutdown:

Code: Select all

erase "%temp%\lock1.txt"

if exist "%temp%\lock1.txt" goto end
if exist "%temp%\lock2.txt" goto end

shutdown /s

:end
2) If they won't call scripts you could just run the erase command on exit and run the script which looks for the locks for both tasks on a repeating schedule using the task scheduler. It should trigger a shutdown when both files are gone. Don't forget though that this will need to be managed somehow so that it doesn't shutdown the system unexpectedly at other times!

3) If neither of those is suitable you could check out the shutdown api at https://learn.microsoft.com/en-us/windo ... -functions

Re: Manage multiple programs which want to shut down Windows

Posted: Tue Nov 28, 2023 12:38 pm
by jacdelad
Thanks you all, but my question is specifically whether Windows offers something or not.
Following spikeys link tells me there really isn't . Also I assume other developers would have adapted it already.
Though, the function ShutdownBlockReasonCreate_() seems interesting. Maybe a program written by myself to manage the shutdown process is still the best option. For that, monitoring the long running programs should already be enough.