v1版:

#SingleInstance Force
SetBatchLines -1
; 优点:不强占线程的情况下,保持一定的高精度。缺点:精度在+1ms内

计时()
Sleep3(3000)
MsgBox % "耗时:" 计时() " 毫秒"
Return


Sleep3(t) {
  static ht := DllCall("CreateWaitableTimerExW", "ptr", 0, "ptr", 0, "uint", 3, "uint", 0x1F0003, "uptr")
  st := DllCall("SetWaitableTimer", "uptr", ht, "uint64*", t * -10000, "int", 0, "ptr", 0, "ptr", 0, "int", 0)
  QS_ALLINPUT := (((0x0002 | 0x0004) | 0x0001 | 0x0400 | 0x0800 | 0x1000) | 0x0008 | 0x0010 | 0x0020 | 0x0080 | 0x0040), WAIT_OBJECT_0 := 0
  While (DllCall("MsgWaitForMultipleObjects", "uint", 1, "ptr*", ht, "int", false, "uint", t, "uint", QS_ALLINPUT) != WAIT_OBJECT_0)
    Sleep -1
  ; (Delete=1 && DllCall("CloseHandle", "uptr", ht))
}

计时() {
  Static
  if 开始=
    DllCall("QueryPerformanceFrequency", "Int64*", 频率), DllCall("QueryPerformanceCounter", "Int64*", 开始)
   else
    DllCall("QueryPerformanceCounter", "Int64*", 结束), 耗时:=(结束 - 开始)/频率*1000, 开始:=""
  Return 耗时
}

 

v2版:

#Requires AutoHotkey v2.0

Sleep3(3000)  ; By Zzz
MsgBox


Sleep3(t) {
  static ht := DllCall("CreateWaitableTimerExW", "ptr", 0, "ptr", 0, "uint", 3, "uint", 0x1F0003, "uptr")
  st := DllCall("SetWaitableTimer", "uptr", ht, "uint64*", t * -10000, "int", 0, "ptr", 0, "ptr", 0, "int", 0)
  QS_ALLINPUT := (((0x0002 | 0x0004) | 0x0001 | 0x0400 | 0x0800 | 0x1000) | 0x0008 | 0x0010 | 0x0020 | 0x0080 | 0x0040), WAIT_OBJECT_0 := 0
  while(DllCall("MsgWaitForMultipleObjects", "uint", 1, "ptr*", ht, "int", false, "uint", t, "uint", QS_ALLINPUT) != WAIT_OBJECT_0)
    sleep(-1)
  static delete := {__Delete : (_) => (DllCall('CloseHandle', "uptr", ht), MsgBox('delete'))}
}

 

另一个精准休眠延时函数链接

Sleep精准休眠延时函数2

声明:站内资源为整理优化好的代码上传分享与学习研究,如果是开源代码基本都会标明出处,方便大家扩展学习路径。请不要恶意搬运,破坏站长辛苦整理维护的劳动成果。本站为爱好者分享站点,所有内容不作为商业行为。如若本站上传内容侵犯了原著者的合法权益,请联系我们进行删除下架。