Jump to content

timer not working correctly


Nhardel
 Share

Recommended Posts

I am trying to write a script that keeps accurate time regardless of how long functions take to preform their actions.

I thought this would be pretty straight forward. I use several while loops to keep track of days, hours, mins, and secs. But for some reason it is not working correctly.

Global $day, $hour, $min, $sec
 
$time = TimerInit()
While 1 ;Main Loop
$hour = 0 ;Reset hours
While  $hour <= 23;Loop while hours is less than 24 hours
  $min = 0 ;Reset minutes
  While $min <= 59 ;Loop while minutes is less than 60 minutes
   $sec = 0 ;Reset seconds
   While $sec <= 59 ;Loop while seconds is less than 60 seconds
    $show_sec = $sec
    $dif = TimerDiff($time) ;checks how many milliseconds has passed since timer start
    $sec = int($dif/1000) ;counts the seconds
    sleep(50)   ;cpu sleep
    if $sec <> $Show_Sec then ConsoleWrite($sec&@crlf)
   WEnd ;End Sec Loop
   $time = $time + 60 * 1000 ; add one minute to initial time to keep accuracy
   $min = $min + 1 ;Add 1 minute
   Consolewrite("!Min: "&$min&@crlf)
  WEnd ;End Min Loop
  $hour = $hour + 1 ;Add 1 hour
WEnd ;End Hour loop
$day = $day + 1 ; Add 1 day
WEnd

[autoit]

For the first minute it goes according to plan but after that :D

For some reason it does not reset the sec to 0 after the first minute. I am sure that this is tied to the $time = $time + 60 *1000 I don't believe that I am adding 60 seconds to the ticks from the init time but this is what the autoit examples show on how to add ticks. Any ideas?

Link to comment
Share on other sites

  • Moderators

nhardel,

I am sure that this is tied to the $time = $time + 60 *1000

Correct. Where do you get the idea that adding something to the returned value from TimerInit is acceptable? :D

A quote from one of the Devs:

"You are trying to attach meaning to the return value of TimerInit() that we do not guarantee. The return value of TimerInit() should be treated as an opaque handle. That means the underlying type is irrelevant. If I want to make it a string that's my choice as a developer of AutoIt. All I have to ensure is that TimerDiff() still accepts that data and that TimerDiff() returns the calculated difference. There are no other guarantees about how either function works."

So replace that line with a new TimerInit call and your timer works fine:

;$time = $time + 60 * 1000 ; add one minute to initial time to keep accuracy
$time = TimerInit()

this is what the autoit examples show on how to add ticks

Can you tell us where you saw this as it is certainly not correct - as you can see from the above - and we will need to change it if it can be interpreted as such! :oops:

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

TimerInit()

Parameters

None.

Return Value

Returns a timestamp number (in milliseconds).

TimerDiff

Parameters

timestamp timestamp returned from a previous call to TimerInit().

Return Value

Returns the time difference (in milliseconds) from a previous call to TimerInit().

Sleep(delay)

Parameters

delay Amount of time to pause (in milliseconds).

Return Value

None.

My own assumption: sleep(5 *1000) == sleep(5000) == sleeping 5 seconds

based on that logic

$time = $time + 60 *1000 should add 60 seconds to the timestamp that TimerInit produced.

The problem with calling the TimerInit again after my 60 seconds elapsed is that the timestamp could be off several hundred milliseconds based on timing of functions outside of the seconds loop. This differenced would exponentially grow until I am out of sync with time by seconds, minutes, hours, and days based on when the application started. Granted this is worse case scenario but a possibility none the less. Perhaps I made a leap too far for what was going on but I feel that my examples easily show how someone could make these assumptions based on of how TimerInit works. I was not aware of the developers comments. Yes, I am aware of "how assumptions make an ass out of, well pretty much me this time" Moving forward How would you go about measuring time regardless of the lapse of time that is not measured while a func is off running doing its thing.

Link to comment
Share on other sites

  • Moderators

nhardel,

Unfortunately you fit the "assumption" definition perfectly - but do not worry, you are by no means the first! :rip:

But you might be the last - as the new Beta has this in the Help file:

Return Value
 
Returns a handle that can be passed to TimerDiff() to calculate the difference in milliseconds.

No mention of units in the return value any more. :oops:

How would you go about measuring time regardless of the lapse of time that is not measured while a func is off running doing its thing

Much the same as you are doing at present - use TimerInit and TimerDiff and convert the difference into HH:MM::SS. Are you finding this does not work for you? :D

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

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

×
×
  • Create New...