Command line 在后台运行计时器的批处理。怎样

Command line 在后台运行计时器的批处理。怎样,command-line,batch-file,timer,multitasking,Command Line,Batch File,Timer,Multitasking,我想知道是否有一种方法可以创建一个使用计时器脚本(比如TIMEOUT命令)的批处理文件,并且在计时器运行时,让批处理文件执行其他进程。但当计时器用完时,退出。(或执行另一个小流程)我知道这意味着te批处理文件必须同时运行多个流程,我只是想知道这是否可能 有什么想法吗?您可以使用启动/b cmd/c myTimeout.bat 这将在同一窗口中启动新应用程序(开始/?) 使用此机制,您应该能够同时执行多个作业 编辑 对于一个简单的超时,这似乎有点过大。 对于6秒的超时,您可以使用 set star

我想知道是否有一种方法可以创建一个使用计时器脚本(比如TIMEOUT命令)的批处理文件,并且在计时器运行时,让批处理文件执行其他进程。但当计时器用完时,退出。(或执行另一个小流程)我知道这意味着te批处理文件必须同时运行多个流程,我只是想知道这是否可能


有什么想法吗?

您可以使用启动/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

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

您可以使用开始/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

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

修改了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

修改了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

start将打开一个新的(命令行)窗口,它不会在当前窗口中启动应用程序。但我同意,这是在背景中开始某件事的唯一途径——至少在背景中Windows@a_horse_with_no_name:start with/b在同一窗口中启动OK谢谢,那么我如何在计时器完成后使程序转到脚本的另一部分?(示例?)start将打开一个新的(命令行)窗口,它不会在“当前”窗口中启动应用程序。但我同意,这是在背景中开始某件事的唯一途径——至少在背景中Windows@a_horse_with_no_name:start with/b在同一窗口中启动OK谢谢,那么我如何在计时器完成后使程序转到脚本的另一部分?(例子?)