Manage multiple programs which want to shut down Windows

For everything that's not in any way related to PureBasic. General chat etc...
User avatar
jacdelad
Addict
Addict
Posts: 1402
Joined: Wed Feb 03, 2021 12:46 pm
Location: Planet Riesa
Contact:

Manage multiple programs which want to shut down Windows

Post 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.
PureBasic 6.04/XProfan X4a/Embarcadero RAD Studio 11/Perl 5.2/Python 3.10
Windows 11/Ryzen 5800X/32GB RAM/Radeon 7770 OC/3TB SSD/11TB HDD
Synology DS1821+/36GB RAM/130TB
Synology DS920+/20GB RAM/54TB
Synology DS916+ii/8GB RAM/12TB
User avatar
ChrisR
Addict
Addict
Posts: 1098
Joined: Sun Jan 08, 2017 10:27 pm
Location: France

Re: Manage multiple programs which want to shut down Windows

Post 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
User avatar
spikey
Enthusiast
Enthusiast
Posts: 575
Joined: Wed Sep 22, 2010 1:17 pm
Location: United Kingdom

Re: Manage multiple programs which want to shut down Windows

Post 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
User avatar
jacdelad
Addict
Addict
Posts: 1402
Joined: Wed Feb 03, 2021 12:46 pm
Location: Planet Riesa
Contact:

Re: Manage multiple programs which want to shut down Windows

Post 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.
PureBasic 6.04/XProfan X4a/Embarcadero RAD Studio 11/Perl 5.2/Python 3.10
Windows 11/Ryzen 5800X/32GB RAM/Radeon 7770 OC/3TB SSD/11TB HDD
Synology DS1821+/36GB RAM/130TB
Synology DS920+/20GB RAM/54TB
Synology DS916+ii/8GB RAM/12TB
Post Reply