Jump to content

how to call accurate sleep


 Share

Recommended Posts

On 2011/12/19 at 3:05 AM, KaFu said:

Give this one a try...

 

 

$h_DLL_NtDll = DllOpen("ntdll.dll")

$Delay_value = GUICtrlRead($Delay)
If WinExists("Notepad") Then
    WinActivate("Notepad")
    _HPSleep($Delay_value)
Else
    MsgBox(0, "Wrong Window", "Open Up Nitto n00b")
EndIf

Func _HPSleep($fMs = 1)
    ; http://www.autoitscript.com/forum/topic/77905-sleep-down-to-100-nano-seconds/page__view__findpost__p__862689
    ; default is milliseconds, otherwise microseconds (1 ms = 1000 µs)
    DllCall($h_DLL_NtDll, "dword", "NtDelayExecution", "int", 0, "int64*", -10000 * $fMs) ; ~ 1ms
EndFunc   ;==>_HPSleep

Why the Second param *(-1)

Link to comment
Share on other sites

https://forum.powerbasic.com/forum/user-to-user-discussions/source-code/56824-ntdelayexecution-sleep-sleepex-with-higher-resolution-absolute-delay

Quote

NtDelayExecution() takes a 64-bit integer (QUAD) as the time-to-sleep, vs 32-bit (DWORD) for Sleep()/SleepEx().

If this is a NEGATIVE integer it specifies the relative time to sleep (in units of 100 nanoseconds), otherwise it specifies the absolute time to wake up (the latter being a feature of it which neither Sleep()/SleepEx() cater for -- we have to call NtDelayExecution directly to use that feature).

Example, using relative time, for the equivalent to Sleep(2500), ie. sleep for 2.5 seconds, we specify -25000000 as the interval with NtDelayExecution(). Using absolute time (which is exclusive to NtDelayExecution and cannot be used via Sleep/SleepEx) we simply get the current system time (UTC, so we use GetSystemTime not GetLocalTime), convert that to filetime, and add 25000000 to the dwLowDateTime field.

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...