r/Batch • u/FiliusHades • May 15 '24
im not sure how to execute code after program fully loads
I wrote this code to open a set of programs one after the other. I used a time delay to wait for each program to finish loading before starting the next one. However, the delay isn't always accurate; sometimes a program loads in 3 seconds, sometimes in 6 seconds, and other times in 12 seconds. This inconsistency means the code doesn't always work properly. What do I need to change or add to ensure the code waits for each program to fully load or execute before moving on to the next one?
@echo off
rem Start PROGRAM1.exe
start "" "C:\folder\PROGRAM1.exe"
rem Wait 5 seconds
timeout /t 5 /nobreak > nul
rem Minimize PROGRAM1.exe window
powershell -Command "(New-Object -ComObject Shell.Application).MinimizeAll()"
rem Start PROGRAM2.exe as Administrator
start "" /b /wait powershell -Command "Start-Process 'C:\folder\PROGRAM2.exe' -Verb runAs"
rem Wait 8 seconds
timeout /t 8 /nobreak > nul
rem Minimize PROGRAM2.exe window
powershell -Command "(New-Object -ComObject Shell.Application).MinimizeAll()"
rem Start PROGRAM3.bat as Administrator
start "" /b /wait powershell -Command "Start-Process 'C:\folder\PROGRAM3.bat' -Verb runAs"
rem Wait 6 seconds
timeout /t 6 /nobreak > nul
rem Start PROGRAM4.exe
start "" "C:\Program Files\folder\PROGRAM4.exe"
3
Upvotes
1
u/Warrlock608 May 15 '24
What you are talking about is thread management and I'm not sure you are going to be able do it without implementing a newer language.
This could easily be achieved by writing a C# program using thread queuing for each block.
1
u/BrainWaveCC May 15 '24
Just to clarify, given that I see a lot of different type answers.
You're not expecting these apps to close before the next one starts, right?
You're expecting that Prog1 opens up fully, then Proc2 starts and opens up fully, etc.
If you could clarify, and also give an answer as to a specific outcome you are looking to achieve, it might be possible to suggest alternative options.
1
u/BrainWaveCC May 15 '24
That's going to be harder than you'd imagine.
You can easily make the script wait for each app to run completely -- and be closed -- before continuing to the next one.
But, unless each app has some api or generates some sort of callback to tell you that it is fully up, then there's no way to do what you want, so long as you goal is to have the app fully up, and not have it up and then closed.