根据sleep 的 Autohotkey 文档, Sleep 命令的时间不准确:
由于操作系统计时系统的粒度,延迟通常向上舍入到最接近的 10 或 15.6 毫秒的倍数(取决于安装的硬件和驱动程序的类型)。例如,在大多数 Windows 2000/XP 系统上,1 到 10(含)之间的延迟相当于 10 或 15.6。要实现更短的延迟,请参阅示例。
如果 CPU 负载不足,实际延迟时间可能会比请求的时间长。这是因为在给脚本另一个时间片之前,操作系统会给每个需要的进程一段 CPU 时间(通常为 20 毫秒)。
这与您的 83 毫秒约为 100 非常吻合。如果您按照该示例链接,您会注意到一个旨在使睡眠时间短于基于系统硬件的 10 或 15.6 毫秒限制的示例:
SetBatchLines -1 ; Ensures maximum effectiveness of this method.
SleepDuration = 1 ; This can sometimes be finely adjusted (e.g. 2 is different than 3) depending on the value below.
TimePeriod = 3 ; Try 7 or 3. See comment below.
; On a PC whose sleep duration normally rounds up to 15.6 ms, try TimePeriod=7 to allow
; somewhat shorter sleeps, and try TimePeriod=3 or less to allow the shortest possible sleeps.
DllCall("Winmm\timeBeginPeriod", uint, TimePeriod) ; Affects all applications, not just this script's DllCall("Sleep"...), but does not affect SetTimer.
Iterations = 83
StartTime := A_TickCount
Loop %Iterations%
DllCall("Sleep", UInt, SleepDuration) ; Must use DllCall instead of the Sleep command.
DllCall("Winmm\timeEndPeriod", UInt, TimePeriod) ; Should be called to restore system to normal.
MsgBox % "Sleep duration = " . (A_TickCount - StartTime) / Iterations
您可以尝试对此进行改编。我已经开始:
SetBatchLines -1 ; This makes your script run at the fastest speed possible, thereby increasing the likelihood that you'll get exactly what time you want to get.
SleepDuration = 1 ; This and the line before it should go at the beginning of your script.
DllCall("Winmm\timeBeginPeriod", uint, TimePeriod) ; this must be before the actual sleep DllCall
Iterations = 83
StartTime := A_TickCount ; You can delete this once you know it works
Loop %Iterations%
DllCall("Sleep", UInt, SleepDuration) ; Must use DllCall instead of the Sleep command.
DllCall("Winmm\timeEndPeriod", UInt, TimePeriod) ; Should be called to restore system to normal, after the middle DllCall.
MsgBox % "Sleep duration = " . (A_TickCount - StartTime) / IterationsMsgBox % "Sleep duration = " . (A_TickCount - StartTime) / Iterations ; Also delete this once you know it works