Jump to content

Pausing Timers


Recommended Posts

I have a script in which I run a loop several thousand times and I have a function that is called on tray mouseover that calculates how many times the loop has run per second. I do this by dividing $i by the timerdif of the timerinit started right before the loop. It takes a little bit of time to do this calculation, so if you leave your mouse over the tray icon, the loops/second rate begins to drop because of the time delay for the calculation.

Is there any way to pause the timer before the calculation and resume it right after the calculation has completed?

Thanks all.

Link to comment
Share on other sites

I have a script in which I run a loop several thousand times and I have a function that is called on tray mouseover that calculates how many times the loop has run per second. I do this by dividing $i by the timerdif of the timerinit started right before the loop. It takes a little bit of time to do this calculation, so if you leave your mouse over the tray icon, the loops/second rate begins to drop because of the time delay for the calculation.

Is there any way to pause the timer before the calculation and resume it right after the calculation has completed?

Thanks all.

You'll need to use a variable as kind of a running total for your timer...it should (naturally) start at 0. Whenever you query the timer, add the actual timer value (timerdiff) to the value stored in your running total variable. When you need to pause the timer, simply add the current timer value to the running total variable. Then when you want to resume the timer, just start it over at 0 (since the previous total time is still stored in the variable). That way, your total time will always be whatever your timer reads plus the value stored in your "running total" variable.

I hope that makes sense...it's late :mellow:

Edit: this might help:

GUICreate("pausable timer",320,60)
Global $TimerLabel=GUICtrlCreateLabel("",5,20,140,20)
Global $TimerPause=GUICtrlCreateButton("Go",165,20,140,20)

Global $RunningTotal=0, $TimerVal, $TimerRunning=0

GUISetState()

While 1
    Switch GUIGetMsg()
        Case -3
            Exit
        Case $TimerPause
            $TimerRunning=NOT $TimerRunning
            If $TimerRunning Then
                GUICtrlSetData($TimerPause,"Pause")
                $TimerVal=TimerInit()
            Else
                GUICtrlSetData($TimerPause,"Resume")
                $RunningTotal+=TimerDiff($TimerVal)
            EndIf
    EndSwitch
    If $TimerRunning Then GUICtrlSetData($TimerLabel,$RunningTotal+TimerDiff($TimerVal))
WEnd
Edited by james3mg
"There are 10 types of people in this world - those who can read binary, and those who can't.""We've heard that a million monkeys at a million keyboards could produce the complete works of Shakespeare; now, thanks to the Internet, we know that is not true." ~Robert Wilensky0101101 1001010 1100001 1101101 1100101 1110011 0110011 1001101 10001110000101 0000111 0001000 0001110 0001101 0010010 1010110 0100001 1101110
Link to comment
Share on other sites

You'll need to use a variable as kind of a running total for your timer...it should (naturally) start at 0. Whenever you query the timer, add the actual timer value (timerdiff) to the value stored in your running total variable. When you need to pause the timer, simply add the current timer value to the running total variable. Then when you want to resume the timer, just start it over at 0 (since the previous total time is still stored in the variable). That way, your total time will always be whatever your timer reads plus the value stored in your "running total" variable.

I hope that makes sense...it's late :mellow:

Edit: this might help:

GUICreate("pausable timer",320,60)
Global $TimerLabel=GUICtrlCreateLabel("",5,20,140,20)
Global $TimerPause=GUICtrlCreateButton("Go",165,20,140,20)

Global $RunningTotal=0, $TimerVal, $TimerRunning=0

GUISetState()

While 1
    Switch GUIGetMsg()
        Case -3
            Exit
        Case $TimerPause
            $TimerRunning=NOT $TimerRunning
            If $TimerRunning Then
                GUICtrlSetData($TimerPause,"Pause")
                $TimerVal=TimerInit()
            Else
                GUICtrlSetData($TimerPause,"Resume")
                $RunningTotal+=TimerDiff($TimerVal)
            EndIf
    EndSwitch
    If $TimerRunning Then GUICtrlSetData($TimerLabel,$RunningTotal+TimerDiff($TimerVal))
WEnd
It's a pity that the Mouse Over event operates repeatedly rather than once when the mouse enters the icon area. Here is one way to achioeve that so you would only need to calculate once when the mouse is over the icon.

#include <Constants.au3>
#include <timers.au3>

Global $mp, $mplastx, $mplasty, $overtime, $Tolerance = 20
Opt("TrayOnEventMode", 1)
Opt("TrayMenuMode", 1); Default tray menu items (Script Paused/Exit) will not be shown.

$exit = TrayCreateItem("Exit")
TrayItemSetOnEvent(-1, "ExitEvent")

TraySetOnEvent($TRAY_EVENT_MOUSEOVER, "SpecialEvent")

TraySetState()

While 1
    Sleep(10); Idle loop
WEnd

Exit

Func CheckOver($a, $b, $c, $d)
    Local $currentmp = MouseGetPos()
    
    If Abs($mplastx - $currentmp[0]) > $Tolerance Or Abs($mplasty - $currentmp[1]) > $Tolerance Then
        _Timer_KillTimer(0, $overtime)
        $overtime = 0
    EndIf
EndFunc  ;==>CheckOver


; Functions
Func SpecialEvent()
    
    Select
        Case @TRAY_ID = $TRAY_EVENT_MOUSEOVER
            If $overtime = 0 Then
                $overtime = _Timer_SetTimer(0, 100, "checkover")
                $mp = MouseGetPos()
                $newover = False
                ConsoleWrite("mouseover" & @CRLF)
            EndIf
            
            $mplastx = $mp[0]
            $mplasty = $mp[1]
        
    EndSelect
EndFunc  ;==>SpecialEvent


Func ExitEvent()
    Exit
EndFunc  ;==>ExitEvent
Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
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...