Search the Community
Showing results for tags 'stopwatch'.
-
This is for my C# fans out there who enjoy OOP. I have created a really rough draft (I mean it's super basic) of the Stopwatch Class in .NET, because I had the idea whilst travelling home and just thought it's neat to show a different paradigm to that of procedural programming. Enjoy. #include <WinAPISys.au3> Global Const $STOPWATCH_GUID = '804835D8-0DF4-11E4-A11F-29560707A45E', $STOPWATCH_TICKSPERMILLISECOND = 10000, $STOPWATCH_TICKSPERSECOND = $STOPWATCH_TICKSPERMILLISECOND * 1000 Global Enum $STOPWATCH_TIMER, $STOPWATCH_RUNNING, $STOPWATCH_ELAPSED, $STOPWATCH_ISHIGHRESOLUTION, $STOPWATCH_FREQUENCY, $STOPWATCH_TICKFREQUENCY, $STOPWATCH_ID, $STOPWATCH_MAX #Region Example Example() Func Example() Local $hStopWatch = Stopwatch() ConsoleWrite('IsRunning: ' & Stopwatch_IsRunning($hStopWatch) & @CRLF) ; Display running status. ConsoleWrite('Started' & @CRLF) Stopwatch_Start($hStopWatch) ; Start the stopwatch. ConsoleWrite('IsRunning: ' & Stopwatch_IsRunning($hStopWatch) & @CRLF) ; Display running status. Sleep(1000) ; Wait for 1 second. Stopwatch_Stop($hStopWatch) ; Stop the stopwatch. ConsoleWrite('Elapsed ms Time: ' & Stopwatch_ElapsedMilliseconds($hStopWatch) & @CRLF) ConsoleWrite('Stopped.' & @CRLF) ConsoleWrite('Waiting for 3 seconds whilst the stopwatch is stopped.' & @CRLF) Sleep(3000) ; Wait for 3 seconds. ConsoleWrite('Started' & @CRLF) Stopwatch_Start($hStopWatch) ; Start the stopwatch again. ConsoleWrite('Waiting for 2 seconds whilst the stopwatch is running' & @CRLF) Sleep(2000) ; Wait for 2 seconds. For $i = 1 To 10 ; The number of milliseconds is shown even when the stopwatch is running. ConsoleWrite('Elapsed ms Time: ' & Stopwatch_ElapsedMilliseconds($hStopWatch) & @CRLF) Sleep(250) Next Stopwatch_Stop($hStopWatch) ; Stop the stopwatch. ConsoleWrite('Elapsed ms Time: ' & Stopwatch_ElapsedMilliseconds($hStopWatch) & @CRLF) ; This should be about 5 seconds or so. ConsoleWrite('Stopped' & @CRLF) ConsoleWrite('IsRunning: ' & Stopwatch_IsRunning($hStopWatch) & @CRLF) ; Display running status. EndFunc ;==>Example #EndRegion Example ; Stopwatch Class: http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch(v=vs.110).aspx Func Stopwatch() Local $aStopwatch[$STOPWATCH_MAX] $aStopwatch[$STOPWATCH_ID] = $STOPWATCH_GUID Stopwatch_Reset($aStopwatch) $aStopwatch[$STOPWATCH_FREQUENCY] = _WinAPI_QueryPerformanceFrequency() If $aStopwatch[$STOPWATCH_FREQUENCY] > 0 Then $aStopwatch[$STOPWATCH_ISHIGHRESOLUTION] = True $aStopwatch[$STOPWATCH_TICKFREQUENCY] = $STOPWATCH_TICKSPERSECOND $aStopwatch[$STOPWATCH_TICKFREQUENCY] /= $aStopwatch[$STOPWATCH_FREQUENCY] Else $aStopwatch[$STOPWATCH_ISHIGHRESOLUTION] = False $aStopwatch[$STOPWATCH_FREQUENCY] = $STOPWATCH_TICKSPERSECOND $aStopwatch[$STOPWATCH_TICKFREQUENCY] = 1 EndIf Return $aStopwatch EndFunc ;==>Stopwatch Func Stopwatch_ElapsedMilliseconds(ByRef $aStopwatch) Return (__Stopwatch_IsStopwatch($aStopwatch) ? __Stopwatch_GetElapsedDateTimeTicks($aStopwatch) / $STOPWATCH_TICKSPERMILLISECOND : 0) EndFunc ;==>Stopwatch_ElapsedMilliseconds Func Stopwatch_ElapsedTicks(ByRef $aStopwatch) Return (__Stopwatch_IsStopwatch($aStopwatch) ? __Stopwatch_GetElapsedDateTimeTicks($aStopwatch) : 0) EndFunc ;==>Stopwatch_ElapsedTicks Func Stopwatch_IsRunning(ByRef $aStopwatch) Return (__Stopwatch_IsStopwatch($aStopwatch) ? $aStopwatch[$STOPWATCH_RUNNING] : Null) EndFunc ;==>Stopwatch_IsRunning Func Stopwatch_Reset(ByRef $aStopwatch) If __Stopwatch_IsStopwatch($aStopwatch) Then $aStopwatch[$STOPWATCH_ELAPSED] = 0 $aStopwatch[$STOPWATCH_RUNNING] = False $aStopwatch[$STOPWATCH_TIMER] = 0 EndIf Return True EndFunc ;==>Stopwatch_Reset Func Stopwatch_Restart(ByRef $aStopwatch) If __Stopwatch_IsStopwatch($aStopwatch) Then Stopwatch_Reset($aStopwatch) Stopwatch_Start($aStopwatch) EndIf Return True EndFunc ;==>Stopwatch_Restart Func Stopwatch_Start(ByRef $aStopwatch) If __Stopwatch_IsStopwatch($aStopwatch) And Not $aStopwatch[$STOPWATCH_RUNNING] Then $aStopwatch[$STOPWATCH_RUNNING] = True $aStopwatch[$STOPWATCH_TIMER] = __Stopwatch_GetTimestamp($aStopwatch) EndIf Return True EndFunc ;==>Stopwatch_Start Func Stopwatch_StartNew() Local $aStopwatch = Stopwatch() Stopwatch_Start($aStopwatch) Return $aStopwatch EndFunc ;==>Stopwatch_StartNew Func Stopwatch_Stop(ByRef $aStopwatch) If __Stopwatch_IsStopwatch($aStopwatch) And $aStopwatch[$STOPWATCH_RUNNING] Then $aStopwatch[$STOPWATCH_ELAPSED] = __Stopwatch_GetRawElapsedTicks($aStopwatch) $aStopwatch[$STOPWATCH_RUNNING] = False EndIf Return True EndFunc ;==>Stopwatch_Stop Func __Stopwatch_GetElapsedDateTimeTicks(ByRef $aStopwatch) Return (__Stopwatch_GetRawElapsedTicks($aStopwatch) * ($aStopwatch[$STOPWATCH_ISHIGHRESOLUTION] ? $aStopwatch[$STOPWATCH_TICKFREQUENCY] : $STOPWATCH_TICKSPERMILLISECOND)) EndFunc ;==>__Stopwatch_GetElapsedDateTimeTicks Func __Stopwatch_GetRawElapsedTicks(ByRef $aStopwatch) Local $iElapsedTime = $aStopwatch[$STOPWATCH_ELAPSED] If $aStopwatch[$STOPWATCH_RUNNING] Then If $aStopwatch[$STOPWATCH_ISHIGHRESOLUTION] Then Local $iTimeStamp = __Stopwatch_GetTimestamp($aStopwatch) $iTimeStamp = $iTimeStamp - $aStopwatch[$STOPWATCH_TIMER] $iElapsedTime += $iTimeStamp Else $iElapsedTime += TimerDiff($aStopwatch[$STOPWATCH_TIMER]) EndIf EndIf Return $iElapsedTime EndFunc ;==>__Stopwatch_GetRawElapsedTicks Func __Stopwatch_GetTimestamp(ByRef $aStopwatch) Return ($aStopwatch[$STOPWATCH_ISHIGHRESOLUTION] ? _WinAPI_QueryPerformanceCounter() : TimerInit()) EndFunc ;==>__Stopwatch_GetTimestamp Func __Stopwatch_IsStopwatch(ByRef $aStopwatch) ; Internal function only. Return (UBound($aStopwatch) = $STOPWATCH_MAX And $aStopwatch[$STOPWATCH_ID] = $STOPWATCH_GUID) EndFunc ;==>__Stopwatch_IsStopwatch
-
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 ........: Chimp ; 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