Jump to content

Simultaneous (Atomic) Reading of Multiple Time Macros


dpryan
 Share

Recommended Posts

Is there a chance of obtaining the wrong time by using time macros?

For example, say I read the time like this:

$time = @HOUR+(@MIN/60)

Suppose the current time is approximately 1:59am, but the clock is in the process of ticking over to 2am.

The @HOUR macro returns 1, but then the clock ticks over and the @MIN macro returns 0. So that gives us a time of 1am instead of ~2am (1 hour too early).

Alternatively, if the @MIN macro is read first, it could return 59. Then as the clock ticks over the @HOUR macro reads 2. So that gives us 2:59am instead of ~2am (1 hour too late).

Is there any way to read the two macros at the same time?

Link to comment
Share on other sites

Autoit is single threaded, you're only going to be able to read one at a time. The scenario you posed is theoretically possible, but the likelyhood of it happening are very very small.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

How is _NowTime() a single "object"??

Func _NowTime($sType = 3)
    If $sType < 3 Or $sType > 5 Then $sType = 3
    Return (_DateTimeFormat(@YEAR & "/" & @MON & "/" & @MDAY & " " & @HOUR & ":" & @MIN & ":" & @SEC, $sType))
EndFunc   ;==>_NowTime

It has exactly the same "problem".

Link to comment
Share on other sites

Haha. You're right. I didn't check the source code.

Here you go...

Based on: http://msdn.microsoft.com/en-us/library/windows/desktop/ms724338%28v=vs.85%29.aspx

MsgBox(0,"Time",_GetLocalTime())
Func _GetLocalTime()
    Local $tSystemTime = DllStructCreate("WORD wYear;WORD wMonth;WORD wDayOfWeek;WORD wDay;WORD wHour;WORD wMin;WORD wSecond;WORD wMilliseconds")
    DllCall('kernel32.dll', 'none', 'GetLocalTime','ptr', DllStructGetPtr($tSystemTime))
    If (@error) Then
        Return SetError(1, 0, 0)
    EndIf
   $hour = DllStructGetData($tSystemTime,"wHour")
   $min = DllStructGetData($tSystemTime,"wMin")
   $sec = DllStructGetData($tSystemTime,"wSecond")
   $time = $hour+$min/60+$sec/3600
   Return $time
EndFunc
Edited by dpryan
Link to comment
Share on other sites

You're still going to have the same "problem" as it takes time to process those commands after you do the DLLCall, so theoretically it will still happen. As I said before I think the likelyhood of it happening is very small, but even with your code, the issue still exists.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

It raises a real question with AFAIK no direct answer. The only way I see is to grab a Windows timestamp (that is an atomic operation) and convert to UTC or local time then dissect the HMS part, just as dpryan did.

@BrewMaNH,

No. There is no such problem with this code, since the timestamp is sampled only once. That it takes several instructions to process properly is irrelevant and won't change the (consistent) content of the structure. The returned values may have some shift (albeit very small) from the timestamp, but it will be consistent.

This might give the substance for a ticket as the current set of macros actually has a consistency problem (when used together to form YMD HMS or part of it).

Edited by jchd

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

Or you could check for the dangerous time and simply wait for 1 or 2 seconds

If @Min = 59 and @sec > 58 Then Sleep((60 - @Sec)*1000)

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

@BrewMaNH,

No. There is no such problem with this code, since the timestamp is sampled only once. That it takes several instructions to process properly is irrelevant and won't change the (consistent) content of the structure. The returned values may have some shift (albeit very small) from the timestamp, but it will be consistent.

I see that now that you mention it, I wasn't thinking about the read once at the time.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

@water: I like your idea of a workaround (for those that don't want to use the dll call method), but sometimes sleeping for several seconds isn't acceptable. I think better way of doing this would be to get @HOUR and @MIN and then @HOUR again and see if @HOUR changed. If so, get @MIN again.

$hour = @HOUR
$min = @MIN
If ($hour <> @HOUR) Then
    $hour = @HOUR
    $min = @MIN
EndIf

@BrewManNM: You say the likelihood is very very small, but what is the probability of it happening? Say I'm using this to create an alarm clock. I'm checking the time in a small loop and when I reach a certain time, I set off the alarm. I don't want to wake up an hour early or an hour late.

So say I'm sampling the time in a 20ms loop. That means that we will take a time measurement within 20ms of the clock ticking over at the critical moment. I have no idea how many instructions it takes to get the @HOUR or @MIN macros. Let's make something up and say there are 40 instructions between. If we have a 2 GHz processor, then I think the probability is somewhere in the range 40 instructions divided by (20ms * 2Ghz) = 1 in a million chance.

Does that make sense? Of course, this is a very simplistic view with nothing else going on in the system. I don't know a lot about operating systems, but I think the scheduling of different processes and other operating system tasks might make it more likely that this problem happens. Also, I think it probably takes way more than 40 instructions to get the time in AutoIt, but I was trying to be conservative on the estimate.

Edited by dpryan
Link to comment
Share on other sites

If you want the alarm to go off at, for example 6:15AM. If you monitor the time such that if the @HOUR is >5 and @Hour < 7 and the @MIN is >=15 I don't see how that would be a problem. If you want it to go off at exactly 6:15 and never 6:16 or higher, then you will need to make sure you're checking the time in a tight enough loop that it checks it in less than 1 minute.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

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...