How do you wait on a Task Scheduler task to finish in a batch file or C#?
You can also get rid of the hacky ping -n command by using timeout
.
Here's the answer of MC ND with timeout
. The 1
in the sample stands for 1 second.
:loop
for /f "tokens=2 delims=: " %%f in ('schtasks /query /tn yourTaskName /fo list ^| find "Status:"' ) do (
if "%%f"=="Running" (
timeout /T 1 /NOBREAK > nul
goto loop
)
)
From batch file, query the task status, and if it is running, keep querying
:loop
for /f "tokens=2 delims=: " %%f in ('schtasks /query /tn yourTaskName /fo list ^| find "Status:"' ) do (
if "%%f"=="Running" (
ping -n 6 localhost >nul 2>nul
goto loop
)
)