Sign in to follow this  
Followers 0
Chimp

simple stopwatch udf

3 posts in this topic

Simple and generalpurpose stopwatch function

the GUI interface is not a "finished product" but a simple example to show how to use the function.

; --- simple example of use ---
#include <GUIConstantsEx.au3>
$Form1_1 = GUICreate("Stopwatch", 170, 70);, 100, 100)
$LapsedTime = GUICtrlCreateLabel("   00:00:00.0", 10, 10, 200, 25)
GUICtrlSetTip(-1, "Lapsed time")
GUICtrlSetFont(-1, 18)
$Button1 = GUICtrlCreateButton("halt/go", 10, 40, 50, 20) ; go / pause
GUICtrlSetTip(-1, "Pause/Unpause counting")
$Button2 = GUICtrlCreateButton("clear", 60, 40, 50, 20) ; reset
GUICtrlSetTip(-1, "Clears counter and stops")
$Button3 = GUICtrlCreateButton("restart", 110, 40, 50, 20) ; restart
GUICtrlSetTip(-1, "Clears counter and restarts")
GUISetState(@SW_SHOW)

Stopwatch(2) ; reset to 0 and stops
While 1
    $x = Int(Stopwatch() / 100)
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1 ; toggle status (go <-> pause)
            Stopwatch() ; this reads counter and sets @extended according to actual $paused status
            Stopwatch(@extended) ;    if is paused (@extended = 1) then unpause [Stopwatch(1)]
            ;                         if is unpaused (@extended = 0) then pause [Stopwatch(0)]
        Case $Button2 ;    reset to 0 and stop counter
            Stopwatch(2)
        Case $Button3 ; reset to 0 and restart counter
            Stopwatch(3)
    EndSwitch

    If $x <> Int(Stopwatch() / 100) Then
        $totsec = Int(Stopwatch() / 1000) ; ms to sec
        $hr = Int($totsec / 3600) ; hours
        $mn = Int(($totsec - ($hr * 3600)) / 60) ; minutes
        $sc = Int(($totsec - ($hr * 3600) - ($mn * 60))) ; seconds
        $tn = Int((Int(Stopwatch() / 100) - ($hr * 36000) - ($mn * 600) - ($sc * 10))) ; tenths of a second
        GUICtrlSetData($LapsedTime, "   " & StringFormat("%02s", $hr) & ":" & StringFormat("%02s", $mn) & ":" & StringFormat("%02s", $sc) & "." & StringFormat("%01s", $tn))
    EndIf
WEnd
; --- end of example ---

; #FUNCTION# (snippet) ==========================================================================================================
; Name...........: Stopwatch
; Description ...: returns the number of milliseconds counted (according to actions requests by caller)
; Syntax.........: Stopwatch([$action])
; Parameters ....: $action: 0 - pause counting
;                           1 - resume counting
;                           2 - reset counter to 0 and stops
;                           3 - reset counter to 0 and start counting
;                           4 - (default) just ruturns actual counting value and status (status in @extended)
;
; Return values .: number of milliseconds in counter
;                  @extended contains paused status (1 or 0)
;                  1 if stopwatch is in pause
;                  0 if stopwatch is counting
;
; Author ........:
; Modified.......:
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......: yes
; ===============================================================================================================================

Func Stopwatch($ToggleTo = 4)
    Static Local $Paused = True
    Static Local $Stopwatch = 0
    Static Local $TotalTime = 0
    Switch $ToggleTo
        Case 0 ; pause counter
            If $Paused Then
                SetExtended($Paused) ; $Paused status
                Return $TotalTime ; already paused, just return current $TotalTime
            Else
                $TotalTime += TimerDiff($Stopwatch)
                $Paused = True
                SetExtended($Paused)
                Return $TotalTime
            EndIf
        Case 1 ; unpause counter
            If $Paused Then
                $Stopwatch = TimerInit()
                $Paused = False
                SetExtended($Paused)
                Return $TotalTime
            Else
                SetExtended($Paused)
                Return $TotalTime + TimerDiff($Stopwatch)
            EndIf
        Case 2 ; reset to 0 and pause
            $Paused = True
            $TotalTime = 0
            SetExtended($Paused)
            Return $TotalTime
        Case 3 ; reset to 0 and restart
            $Paused = False
            $TotalTime = 0
            $Stopwatch = TimerInit()
            SetExtended($Paused)
            Return $TotalTime
        Case 4 ; return status
            SetExtended($Paused)
            If $Paused Then
                Return $TotalTime
            Else
                Return $TotalTime + TimerDiff($Stopwatch)
            EndIf
    EndSwitch
EndFunc   ;==>Stopwatch

P.S.
If you need a finished stopwatch with a nice gui and more functions, take a look to >this one by AZJIO

1 person likes this

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Share this post


Link to post
Share on other sites



#3 ·  Posted (edited)

Thank you :)

P.S.
I wrote that while was experimenting on this phrase found in the "hystory" of AutoIt:

"Functions can now be referenced in variables and the function can be called through the variable."
(wrongly) I thought to be able to instantiate functions and so I tryed this WRONG example of use....
 

; WRONG example of use
; -----

$counter1 = Stopwatch ; instantiate first stopwatch (it doesn't)
$counter2 = Stopwatch ; instantiate second stopwatch (it doesn't)

$counter1(3) ; start counting of the first stopwatch (aint so, first and second are both the same stopwatch)
Sleep(1000) ; wait a second
$counter2(3) ; start counting of the second stopwatch (aint so, first and second are both the same stopwatch)
Sleep(1000) ; wait another second

; my hope was to have 2 results (second timer 1 second late on the first)
; but both refer to the same stopwatch
ConsoleWrite("1) " & $counter1() & @TAB & "2) " & $counter2() & @CRLF)



; #FUNCTION# (snippet) ==========================================================================================================
; Name...........: Stopwatch
; Description ...: returns the number of milliseconds counted (according to actions requests by caller)
; Syntax.........: Stopwatch([$action])
; Parameters ....: $action: 0 - pause counting
;                           1 - resume counting
;                           2 - reset counter to 0 and stops
;                           3 - reset counter to 0 and start counting
;                           4 - (default) just ruturns actual counting value and status (status in @extended)
;
; Return values .: number of milliseconds in counter
;                  @extended contains paused status (1 or 0)
;                  1 if stopwatch is in pause
;                  0 if stopwatch is counting
;
; Author ........:
; Modified.......:
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......: yes
; ===============================================================================================================================

Func Stopwatch($ToggleTo = 4)
    Static Local $Paused = True
    Static Local $Stopwatch = 0
    Static Local $TotalTime = 0
    Switch $ToggleTo
        Case 0 ; pause counter
            If $Paused Then
                SetExtended($Paused) ; $Paused status
                Return $TotalTime ; already paused, just return current $TotalTime
            Else
                $TotalTime += TimerDiff($Stopwatch)
                $Paused = True
                SetExtended($Paused)
                Return $TotalTime
            EndIf
        Case 1 ; unpause counter
            If $Paused Then
                $Stopwatch = TimerInit()
                $Paused = False
                SetExtended($Paused)
                Return $TotalTime
            Else
                SetExtended($Paused)
                Return $TotalTime + TimerDiff($Stopwatch)
            EndIf
        Case 2 ; reset to 0 and pause
            $Paused = True
            $TotalTime = 0
            SetExtended($Paused)
            Return $TotalTime
        Case 3 ; reset to 0 and restart
            $Paused = False
            $TotalTime = 0
            $Stopwatch = TimerInit()
            SetExtended($Paused)
            Return $TotalTime
        Case 4 ; return status
            SetExtended($Paused)
            If $Paused Then
                Return $TotalTime
            Else
                Return $TotalTime + TimerDiff($Stopwatch)
            EndIf
    EndSwitch
EndFunc   ;==>Stopwatch

anyway the resulting function is not bad and (I hope) can have some use..

Edited by Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Share this post


Link to post
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
Sign in to follow this  
Followers 0