开发者

Batch that runs a timer in the background. How?

开发者 https://www.devze.com 2023-01-28 11:25 出处:网络
I was wondering if theres a way to make a batch file that uses a timer script (such as the TIMEOUT command) and while the timer is running, make the batch file do other processes. But when the timer r

I was wondering if theres a way to make a batch file that uses a timer script (such as the TIMEOUT command) and while the timer is running, make the batch file do other processes. But when the timer runs out, then exit. (Or carry out another small process) I know this means t开发者_运维知识库hat te batch file would have to run multiple processes at once, i just want to know if its possible somehow.

Any ideas?


You can use start /b cmd /c myTimeout.bat
This starts a new application in the same window (start /?)

With this mechanism you should be able do multiple jobs at the same time

EDIT For a simple timeout this seems to be a bit oversized. For a timeout of 6 seconds you can use something like

set startTime=%time%
call :timeToMs startMs startTime
set /a timeoutAt=startMs + 6000
:loop
... wait for something, like a file is created, or a CD is inserted
if job_is_ready goto :leaveTheLoop
REM Test if the timeout is reached
set currentTime=%time%
call :timeToMs nowMs currentTime
if nowMs GTR timeoutAt goto :leaveTheLoop
goto :loop

:leaveTheLoop

It fits better for real parallel jobs, like a display job and a key-input job for a game.


modified the code from jeb so that it works:

@echo off
set /A startMs=10*%time:~9,2%+1000*%time:~6,2%+60000*%time:~3,2%+3600000*%time:~0,2%
REM the 6000 in the next line is how many milliseconds of a delay you want
set /a timeoutAt=%startMs%+6000
:loop
REM 'passive' code here:

REM Test if the timeout is reached
2>NUL set /A nowMs=10*%time:~9,2%+1000*%time:~6,2%+60000*%time:~3,2%+3600000*%time:~0,2%
if /I %nowMs% GTR %timeoutAt% goto leaveTheLoop
goto loop

:leaveTheLoop
REM do the things you want to do when the timer ends
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号