从Windows Vista开始,你可以使用TIMEOUT和SLEEP命令,但要在Windows XP或Windows Server 2003上使用它们,你需要Windows Server 2003资源工具包。
这里有一个很好的睡眠替代方案概述(ping方法最流行,因为它适用于每台Windows机器),但还有(至少)一种未被提及的方法,即(滥用)W32TM
(时间服务)命令:
w32tm /stripchart /computer:localhost /period:1 /dataonly /samples:N >nul 2>&1
在这里,您应该将N替换为您想要暂停的秒数。此外,它将在没有先决条件的情况下在每个Windows系统上运行。
Typeperf也可以使用:
typeperf "\System\Processor Queue Length" -si N -sc 1 >nul
使用mshta和javascript(可用于小于一秒的睡眠):
start "" /wait /min /realtime mshta "javascript:setTimeout(function(){close();},5000)"
这应该更加精确(等待不到一秒钟)- 依赖于.net
的自编译可执行文件:
@if (@X)==(@Y) @end /* JScript comment
@echo off
setlocal
::del %~n0.exe /q /f
::
:: For precision better call this like
:: call waitMS 500
:: in order to skip compilation in case there's already built .exe
:: as without pointed extension first the .exe will be called due to the ordering in PATEXT variable
::
::
for /f "tokens=* delims=" %%v in ('dir /b /s /a:-d /o:-n "%SystemRoot%\Microsoft.NET\Framework\*jsc.exe"') do (
set "jsc=%%v"
)
if not exist "%~n0.exe" (
"%jsc%" /nologo /w:0 /out:"%~n0.exe" "%~dpsfnx0"
)
%~n0.exe %*
endlocal & exit /b %errorlevel%
*/
import System;
import System.Threading;
var arguments:String[] = Environment.GetCommandLineArgs();
function printHelp(){
Console.WriteLine(arguments[0]+" N");
Console.WriteLine(" N - milliseconds to wait");
Environment.Exit(0);
}
if(arguments.length<2){
printHelp();
}
try{
var wait:Int32=Int32.Parse(arguments[1]);
System.Threading.Thread.Sleep(wait);
}catch(err){
Console.WriteLine('Invalid Number passed');
Environment.Exit(1);
}
ping
命令模拟睡眠,或下载包含sleep
命令的 Windows 资源工具包。更多详情请参见:批处理文件 SLEEP 命令。 - Rudu