#Region ;**** Directives created by AutoIt3Wrapper_GUI **** #AutoIt3Wrapper_Outfile=TimerTest_v0.0.exe #AutoIt3Wrapper_Run_Au3Stripper=y #Au3Stripper_Parameters=/sv #EndRegion ;**** Directives created by AutoIt3Wrapper_GUI **** ; ============================================================================= ; Timer Test ; ============================================================================= #Region INCLUDE #include #include #include #include #include #include "ColorConstants.au3" #include "Timers.au3" ;see https://www.autoitscript.com/forum/topic/185229-how-do-you-use-a-timer/ #EndRegion INCLUDE $hGuiParent = GUICreate("TimerTest", 520, 180) Local $idMenu1 = GUICtrlCreateMenu("&File") Local $exitMenuItem = GUICtrlCreateMenuItem('E&xit', $idMenu1) #Region GUI_CONTROLS Local const $InputLeftSide = 128 Local const $LabelLeftSide = 28 const $TabTop = 5 const $BottomTop = 240 ; under the tabs area Local $CurTop = 40 GUICtrlCreateTab( 10, $TabTop, 480, 120) $GenTab = GUICtrlCreateTabItem(" Timer Status ") Local $timeButton = GUICtrlCreateButton("Timer Reset", 220, $CurTop, 120, 30) GUICtrlSetTip(-1, 'Reset the timer') GUICtrlSetState ($timeButton, $GUI_ENABLE) $CurTop += 40 Local $DataField = GUICtrlCreateInput("", $LabelLeftSide, $CurTop, 260, 22) GUICtrlSetTip(-1, 'Text box, repurposed for test') GUICtrlCreateTabItem("") ; end tabitem definition $CurTop += 50 Local $statusLabel = GUICtrlCreateLabel("Not connected", $LabelLeftSide + 60, $CurTop + 10, 290, 30) GUICtrlSetTip(-1, 'Status updates') GUICtrlSetColor ( $statusLabel, $COLOR_RED ) #EndRegion GUI_CONTROLS Local $dummyCtrl = 0 ; when nothing happens, it says event 0 happened Local $timerId Local $lastMsgTime $lastMsgTime = TimerInit() StartTimer() GUISetState(@SW_SHOW) While 1 Local $index Local $retval Switch GUIGetMsg() ;===================== ; GUI-wide controls ;===================== Case $GUI_EVENT_CLOSE ExitLoop case $exitMenuItem ExitLoop case $timeButton ResetLastMsgTimer() case $dummyCtrl ; when nothing happens, it says event 0 happened ; catch it here instead of a button that hasn't been defined yet EndSwitch ; _Timer_KillAllTimers($hGuiParent) WEnd _Timer_KillTimer($hGuiParent, $timerId) GUIDelete() ; ============================================================================= ; write custom message to the status bar ; ============================================================================= Func WriteStatusMsg ($color, $text) ; put on status bar GUICtrlSetData ($statusLabel, $text) GUICtrlSetColor ($statusLabel, $color) EndFunc #Region Timer ; ============================================================================= ; Start timer to check connection to a reader ; ============================================================================= Func StartTimer() ; ConsoleWrite ("==> Creating timer" & @CRLF) $timerId = _Timer_SetTimer($hGuiParent, 2000, "CheckConnection") If $timerId == 0 Then WriteStatusMsg ($COLOR_RED, "Couldn't start timer") Else WriteStatusMsg ($COLOR_GREEN, "Timer Started") EndIf EndFunc ; ============================================================================= ; Reset timer when a msg was received ; ============================================================================= Func ResetLastMsgTimer() $lastMsgTime = TimerInit() ConsoleWrite ("==> timer time " & $lastMsgTime & @CRLF) If $lastMsgTime == 0 Then WriteStatusMsg ($COLOR_RED, "Couldn't reset timer") EndIf EndFunc ; ============================================================================= ; Start timer to check connection ; ============================================================================= Func CheckConnection($hWnd, $iMsg, $iIDTimer, $iTime) Local $timeDiff = TimerDiff($lastMsgTime) ConsoleWrite ("==> Time since last msg is " & $timeDiff & @CRLF) If $timeDiff > 2000 Then GUICtrlSetData ($DataField, $timeDiff) WriteStatusMsg ($COLOR_GREEN,"Timer is reset") EndIf EndFunc #EndRegion