gpt4 book ai didi

batch-file - 在一秒钟内延迟批处理文件?

转载 作者:行者123 更新时间:2023-12-04 11:04:35 25 4
gpt4 key购买 nike

有什么方法可以将批处理文件延迟一秒钟,例如10毫秒吗?

我试图写这个:

ping localhost -n 1 -w 10

但这没用

有人可以帮忙吗?

最佳答案

前段时间I posted a method提供了精确的定时,延迟间隔从15毫秒起。这是整个帖子的副本。

我认为我在延迟小的时候实现了毫秒级的延迟,并具有精确的定时。我使用了带有WScript.Sleep方法的混合Batch-JScript解决方案,但是为了避免每次使用JScript部分时的加载延迟,两个部分必须同时处于事件状态。 JScript进程以毫秒为单位进行延迟,执行延迟并将信号发送到Batch部分。批处理过程将毫秒数发送到JScript并等待信号。实现这种双向通信的方法是通过JScript的WshShwll.Exec方法访问批处理的Stdin和Stdout流。

@if (@CodeSection == @Batch) @then


@echo off
setlocal EnableDelayedExpansion
if defined restart goto secondPart

rem First part: execute JScript section, so it re-execute this Batch file
set restart=true
CScript //nologo //E:JScript "%~F0" "%~F0"
goto :EOF

:secondPart

rem To do a delay, use: "echo #millisecs" followed by "set /P ="; use "echo 0" to end
rem To display data in the screen, use: echo data > CON
rem To read data from keyboard, use set /P "data=Prompt: " < CON > CON

set runs=10
For %%t in (5 10 15 20 30 50 100 250 500 1000) do (

set time_idle_ms=%%t
(
set t0=!time!
for /L %%p in (1,1,%runs%) do echo %%t& set /P =
set t1=!time!
)

for /F "tokens=1-8 delims=:.," %%a in ("!t0: =0!:!t1: =0!") do (
set /a "a=(((1%%e-1%%a)*60)+1%%f-1%%b)*6000+1%%g%%h-1%%c%%d, a+=(a>>31) & 8640000"
)

set /a average_time=a*10/runs
echo(Input:!time_idle_ms! ms - Output: Average time !average_time! ms > CON
)

rem Send the signal to end JScript section
echo 0
goto :EOF


@end


// JScript section

// Restart this Batch file with access to its Stdin and Stdout streams
var WshShell = new ActiveXObject("WScript.Shell");
var BatchFile = WshShell.Exec('"'+WScript.Arguments(0)+'"'), delay;

// Get delay, wait and send CR until delay equ 0
while ((delay = BatchFile.Stdout.ReadLine()) != "0" ) {
WScript.Sleep(delay);
BatchFile.Stdin.WriteLine();
}

输出:
Input:5 ms - Output: Average time 15 ms
Input:10 ms - Output: Average time 16 ms
Input:15 ms - Output: Average time 15 ms
Input:20 ms - Output: Average time 32 ms
Input:30 ms - Output: Average time 31 ms
Input:50 ms - Output: Average time 63 ms
Input:100 ms - Output: Average time 109 ms
Input:250 ms - Output: Average time 250 ms
Input:500 ms - Output: Average time 500 ms
Input:1000 ms - Output: Average time 1000 ms

Windows 8.1 32位-3.2 GHz中的另一项测试
Input:5 ms - Output: Average time 14 ms
Input:10 ms - Output: Average time 16 ms
Input:15 ms - Output: Average time 15 ms
Input:20 ms - Output: Average time 31 ms
Input:30 ms - Output: Average time 32 ms
Input:50 ms - Output: Average time 61 ms
Input:100 ms - Output: Average time 110 ms
Input:250 ms - Output: Average time 250 ms
Input:500 ms - Output: Average time 501 ms
Input:1000 ms - Output: Average time 1000 ms

编辑:已添加 pathping测试

为了完成本主题,我使用 pathping和用于测试我的方法的相同代码进行了时序测试。这里是:
@echo off
setlocal EnableDelayedExpansion

set runs=10
For %%t in (5 10 15 20 30 50 100 250 500 1000) do (

set time_idle_ms=%%t
(
set t0=!time!
for /L %%p in (1,1,%runs%) do pathping 127.0.0.1 -n -q 1 -p %%t >nul
set t1=!time!
)

for /F "tokens=1-8 delims=:.," %%a in ("!t0: =0!:!t1: =0!") do (
set /a "a=(((1%%e-1%%a)*60)+1%%f-1%%b)*6000+1%%g%%h-1%%c%%d, a+=(a>>31) & 8640000"
)

set /a average_time=a*10/runs
echo(Input:!time_idle_ms! ms - Output: Average time !average_time! ms

)

结果表明,对于短的延迟时间, pathping是不可靠的:
Input:5 ms - Output: Average time 48 ms
Input:10 ms - Output: Average time 47 ms
Input:15 ms - Output: Average time 47 ms
Input:20 ms - Output: Average time 62 ms
Input:30 ms - Output: Average time 63 ms
Input:50 ms - Output: Average time 93 ms
Input:100 ms - Output: Average time 141 ms
Input:250 ms - Output: Average time 281 ms
Input:500 ms - Output: Average time 532 ms
Input:1000 ms - Output: Average time 1031 ms

关于batch-file - 在一秒钟内延迟批处理文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29732878/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com