在后台运行计时器的批处理。怎么样?

问题描述 投票:2回答:2

我想知道是否存在一种使用计时器脚本(例如TIMEOUT命令)制作批处理文件的方法,并且在计时器运行时,使该批处理文件执行其他处理。但是,当计时器用尽时,请退出。 (或者执行另一个小的过程)我知道这意味着批处理文件必须一次运行多个进程,我只是想知道它是否可能。

有什么想法吗?

command-line batch-file timer multitasking
2个回答
0
投票

您可以使用start / b cmd / c myTimeout.bat这将在同一窗口中启动一个新应用程序(开始/?)

使用这种机制,您应该能够同时执行多项工作

编辑对于简单的超时,这似乎有点过大。对于6秒钟的超时,您可以使用类似

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

它更适合真正的并行作业,例如显示作业和游戏的按键输入作业。


0
投票

修改了jeb的代码,使其起作用:

@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
© www.soinside.com 2019 - 2024. All rights reserved.