Angler Posted December 10, 2015 Share Posted December 10, 2015 (edited) 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) EndFuncAnd 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)) EndFuncany ideas? I feel like it's probably something super simple but I just can't tell. Edited December 10, 2015 by Angler Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted December 10, 2015 Moderators Share Posted December 10, 2015 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:expandcollapse popup#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 WEndM23 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 columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
Angler Posted December 11, 2015 Author Share Posted December 11, 2015 Ah yes thank you so much, different perspectives are amazing (change the end rather then the start) and I never understood how GUI's worked until this little example, thank you so much! Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted December 11, 2015 Moderators Share Posted December 11, 2015 Angler,Delighted I could help.M23 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 columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now