.
Re: How to Run at Specific Time in Milliseconds?
If you need timers more accurate than the minimum granularity of the system timer (~10ms) then you can use MicroTimer.
Re: How to Run at Specific Time in Milliseconds?
You don't have to check time constantly for the whole period of wait... just for a few 10s of ms.GT77 wrote:I tried the script and I noticed that it always fires few houndreds milliseconds late, which means it can be as late as 999 milliseconds, which means it can be late almost a full second. Is it impossible to do using A_TickCount or other functions and NOT checking time constantly?
Try this.
Code: Select all
T := A_Now
T += 1, Seconds
T .= ".500"
MsgBox % SleepTill( T )
SleepTill( Timestamp ) {
Local OTC := TC := A_TickCount, V := StrSplit( TimeStamp ".0", "." ), SleepMS := V.1
SleepMS -= A_Now, Seconds
SleepMS := ( SleepMS * 1000 + V.2 )
TC := TC + SleepMS
Sleep, %SleepMS%
While ( A_Tickcount < TC )
Sleep -1
Return ( A_Tickcount - OTC ) / 1000
}
You could call it like.. for eg.
Code: Select all
SleepTill( 20170818054334.500 )
SetTimer, Alarm, -1
SleepTill() function can be be precise to 1ms if query performance counters are used. See AccuSleep()
Re: How to Run at Specific Time in Milliseconds?
The problem isn't the accuracy of Sleep/SetTimer, it's the calculation of DelayInMilliseconds/Period.GT77 wrote:I noticed that it always fires few houndreds milliseconds late, which means it can be as late as 999 milliseconds, which means it can be late almost a full second.
Code: Select all
TargetTime := 20170818000010
CurrentTime := 20170818000000 ; replaces A_Now
TimeToWait := TargetTime
TimeToWait -= CurrentTime, Seconds
TimeToWait *= 1000 ; convert to milliseconds
MsgBox, %TimeToWait%
If calculated at CurrentTime + 0 ms it will expire about TargetTime + 0 ms which is the wanted behaviour. But if calculated at CurrentTime + 999 ms it will expire about TargetTime + 999 ms which is correct but unwanted. You have to subtract the current milliseconds in a 'milliseconds until' calculation:
Code: Select all
TimeToWait *= 1000 ; convert to milliseconds
TimeToWait -= CurrentMSec ; subtract current milliseconds
Re: How to Run at Specific Time in Milliseconds?
Kinda A_TickCount/QueryPerformanceCounter scenario ?!You have to subtract the current milliseconds in a 'milliseconds until' calculation
https://autohotkey.com/board/topic/1090 ... r-example/
https://autohotkey.com/board/topic/4806 ... cecounter/
Who is online
Users browsing this forum: Google [Bot], iamMG and 131 guests