Jump to content

Timers acting up


Angler
 Share

Recommended Posts

Alright so I'm trying to have a timer to keep track of a few things, well 3 different timers. I need to be able to adjust them randomly (start, end or somewhere random in between). I used to be able to just add to the handle that I put TimerInit() on, but then that randomly stopped working, and TimerInit/TimerDiff were also was just a bit too wonky for me, sometimes they would only be a few milliseconds off, sometimes they would be up to 10 seconds off. I randomly came across this little code snippet, (forgot who originally put it up, sorry)

Func getTimerDiff($begin)
   $end = getTimer()
   $diff = $end - $begin
   MsgBox(0, "Finished checking time!", "I got: " & $diff & ", as the difference")
EndFunc
Func getTimer($adjustment = 0)
   Local $aResult = DllCall("winmm.dll", "long", "timeGetTime")
   If @error Then Return SetError(@error, @extended, -1)
   Return ($aResult[0] - $adjustment)
EndFunc

And it works perfect as a timer, but no matter what I try I can't seem to adjust it ("speed it up" or "slow it down" although I would only ever need to shorten the time "speed it up")

Func resetClock($clockNum)
   Execute(Eval("clock" & $clockNum) = getTimer())
EndFunc
Func endClock($clockNum)
   Execute(Eval("clock" & $clockNum) = getTimer(Execute("$end" & $clockNum & "[0]")))
EndFunc
Func adjustClock($clockNum, $adjustment = InputBox("Amount to adjust clock by?", "This will account for milliseconds, but you still have to calculate the amount of seconds you have remaining!"))
   Execute(Eval("clock" & $clockNum) = getTimer($adjustment * 1000))
EndFunc

any ideas? I feel like it's probably something super simple but I just can't tell.

Edited by Angler
Link to comment
Share on other sites

  • Moderators

Angler,

I have used TimerInit/Diff for many timers and have never found any "wonky" results. Of course, you need to check the time elapsed regularly, so you must avoid blocking functions - that could perhaps be the explanation for your overruns of several seconds. Here is an example of where you can use the functions to set timers which can be adjusted up and down as you require - if you reduce the delay below the elapsed time the timer exits at once:

#include <GUIConstantsEx.au3>

Global $aTimer[4], $aInput[4], $aUpDown[4], $aButton[4]

$hGUI = GUICreate("Test", 500, 500)

For $i = 1 To 3
    $aInput[$i] = GUICtrlCreateInput(0, 10, (50 * $i) - 20, 100, 40)
    GUICtrlSetFont($aInput[$i], 18)
    $aUpDown[$i] = GUICtrlCreateUpdown($aInput[$i])
    GUICtrlSetLimit(-1, 3600)
    $aButton[$i] = GUICtrlCreateButton("Start", 150, (50 * $i) - 20, 80, 30)
Next

GUISetState()

While 1

    $iMsg = GUIGetMsg()
    Switch $iMsg

        Case $GUI_EVENT_CLOSE
            Exit
        Case Else
            For $i = 1 To 3
                ; If a button is pressed
                If $iMsg = $aButton[$i] Then
                    Switch GUICtrlRead($aButton[$i])
                        Case "Start" ; Start the timer
                            $aTimer[$i] = TimerInit()
                            GUICtrlSetData($aButton[$i], "Stop")
                            ExitLoop

                        Case "Stop" ; Stop the timer
                            GUICtrlSetData($aButton[$i], "Start")
                            GUICtrlSetData($aInput[$i], 0)
                            ExitLoop

                    EndSwitch

                EndIf

            Next
    EndSwitch



    ; Check timers
    For $i = 1 To 3
        ; Check if timer running
        If GUICtrlRead($aButton[$i]) = "Stop" Then
            ; Check if timed out
            If TimerDiff($aTimer[$i]) > Number(GUICtrlRead($aInput[$i])) * 1000 Then
                ConsoleWrite("Timer " & $i & " timed out after " & GUICtrlRead($aInput[$i]) & " seconds" & @CRLF)
                ; Reset timer
                GUICtrlSetData($aButton[$i], "Start")
                GUICtrlSetData($aInput[$i], 0)
            EndIf

        EndIf

    Next
WEnd

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

  • Moderators

Angler,

Delighted I could help.

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

  • Recently Browsing   0 members

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