Jump to content

how to call accurate sleep


Recommended Posts

How can i use this script for the more accurate sleep to sleep the time in an input box

this is the accurate sleep script i found

If you want it even more accurate, you should to all assigning outside of the func:

Func _HighPrecisionSleep(ByRef $aSleep)
    DllCall($aSleep[0],"dword","ZwDelayExecution","int",0,"ptr",$aSleep[2])
EndFunc

Func _HighPrecisionSleep_Init($iMicroSeconds=0)
    Local $aSleep[3] = [DllOpen("ntdll.dll"),DllStructCreate("int64 time;")]
    $aSleep[2] = DllStructGetPtr($aSleep[1])
    DllStructSetData($aSleep[1],"time",-1*($iMicroSeconds*10))
    Return $aSleep
EndFunc

Func _HighPrecisionSleep_Set(ByRef $aSleep, $iMicroSeconds)
    DllStructSetData($aSleep[1],"time",-1*($iMicroSeconds*10))
EndFunc

Func _HighPrecisionSleep_UnInit(ByRef $aSleep)
    DllClose($aSleep[0])
    Dim $aSleep[3] = [0,0,0]
EndFunc

$Sleep = _HighPrecisionSleep_Init(10)

_HighPrecisionSleep($Sleep) ; sleep 10 microseconds

_HighPrecisionSleep_Set($Sleep,100)
_HighPrecisionSleep($Sleep) ; sleep 100 microseconds

I have this in my script

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

instead of the word sleep i want to refer it to the accurate sleep func and wait the time according to whats in the box based on 1000 = 1 sec. I use the Input method so that i can change how long it sleeps per computer i use this on in my gui.

Link to post
Share on other sites
  • Developers

Could you define what is not accurate enough with Sleep() and what kind of accuracy you are looking for?

Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to post
Share on other sites

nothing really is wrong with it I am completely done with my save exe and am exploring what autoit has to offer. I like how easy it is to code and run and stuff. I was just confused how to run it would it look like this

$Delay_value = _HighPrecisionSleep_Init

then in the script write this

_HighPrecisionSleep($Delay_value)

Link to post
Share on other sites

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
Link to post
Share on other sites

no i copied this function in

Func _HPSleep($fMs = 1)
    DllCall($h_DLL_NtDll, "dword", "NtDelayExecution", "int", 0, "int64*", -10000 * $fMs) ; ~ 1ms
EndFunc

but put it at the bottom of my script

then the other part at the top

$Launch_value = GUICtrlRead($Delay)
$h_DLL_NtDll = DllOpen("ntdll.dll")
If GUICtrlRead($Continue) = $GUI_CHECKED Then
   _HPSleep($Launch_value)
   movemouse(1, 100) ; just to make sure the sleep worked and the mouse moves to let me know
EndIf
Link to post
Share on other sites

Yes, put the line

Global $h_DLL_NtDll = DllOpen("ntdll.dll")

somewhere at the top of your script outside of any function and before the first call to _HPSleep() to open the dll. I've done this to speed up the dllcall() function calls. The dll is automatically closed on program exit.

Link to post
Share on other sites

Could you define what is not accurate enough with Sleep() and what kind of accuracy you are looking for?

Jos

nothing really is wrong with it I am completely done with my save exe and am exploring what autoit has to offer. I like how easy it is to code and run and stuff. I was just confused how to run it would it look like this

$Delay_value = _HighPrecisionSleep_Init

then in the script write this

_HighPrecisionSleep($Delay_value)

Yea i did that and it worked, thank you so much both of you. The sleep is very accurate not as much as i need but way better than just the regular sleep.

Okay, so you changed your mind about what you need, nothing wrong with that.

So what is it you need this sleep for? perhaps someone can better help you with this info.

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to post
Share on other sites

nothing really is wrong with it I am completely done with my save exe and am exploring what autoit has to offer. I like how easy it is to code and run and stuff. I was just confused how to run it would it look like this

Aside from that, I am curious about something. I've read that sleep functions cannot return an accurate value for durations less than 15ms. Is this true for all sleep functions. Is it true for AutoIt's Sleep?

Edited by LaCastiglione
Link to post
Share on other sites

Don't know about others. But for AutoIt's Sleep() function. Yep

Timings General Sleep() times might be System depending ... Ergo:

#AutoIt3Wrapper_AU3Check_Parameters=-q -d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 -w 7
#include <array.au3>
_SleepRun()
Func _SleepRun()
    Local Const $iMaxSleepTestValue = 64
    Local Const $iTimingLoopCount = 4

    Local $aResults_Min[$iMaxSleepTestValue + 1]
    Local $iTimer = 0, $iMin = 0

    For $iSleep = 0 To $iMaxSleepTestValue
        $aResults_Min[$iSleep] = Floor(2 ^ 30)
    Next

    For $iSleep = 0 To $iMaxSleepTestValue
        ConsoleWrite('Loop: ' & $iSleep & '/' & $iMaxSleepTestValue & @CRLF)
        $iMin = $aResults_Min[$iSleep]
        For $i2 = 1 To $iTimingLoopCount
            $iTimer = TimerInit()
            Sleep($iSleep)
            $iTimer = TimerDiff($iTimer)
            If $iMin > $iTimer Then $iMin = $iTimer
        Next
        $aResults_Min[$iSleep] = Round($iMin)
    Next
    _ArrayDisplay($aResults_Min)

EndFunc

[Edit1: Always seem to forget to include the #include lines ... ]

[Edit2: ... something ... ]

Edited by MvGulik

"Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions."
"The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014)

"Believing what you know ain't so" ...

Knock Knock ...
 

Link to post
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
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...