I started this recursion mess, so I'll fix it.
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
Opt("GUIOnEventMode", 1) ; Switch to event mode
Global $fRunning = True
Global $fPaused = False
Global $fCounting = False
Global $iCount = 0
$Form1 = GUICreate("Form1", 310, 437, 345, 194)
$Button2 = GUICtrlCreateButton("Pauza", 80, 128, 75, 25, $WS_GROUP)
$Button3 = GUICtrlCreateButton("Exit", 80, 70, 75, 25, $WS_GROUP)
$Button4 = GUICtrlCreateButton("Count", 80, 95, 75, 25, $WS_GROUP)
; Register events
GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit")
GUICtrlSetOnEvent($Button2, "_Pause")
GUICtrlSetOnEvent($Button3, "_Exit")
GUICtrlSetOnEvent($Button4, "_Count")
HotKeySet("p", "_Pause")
GUISetState(@SW_SHOW)
Global $hTimer = TimerInit()
While $fRunning
If Not $fPaused Then ;Only do other stuff when we are not paused
If $fCounting And TimerDiff($hTimer) >= 1000 Then
$hTimer = TimerInit()
$iCount += 1
ToolTip("Time: " & $iCount, 0, 0)
EndIf
EndIf
Sleep(100) ; Sleep to avoid high CPU usage
WEnd
Func _Pause()
$fPaused = Not $fPaused
If Not $fPaused Then
_ShowTip("Program is running", 0, 0)
GUICtrlSetData($Button2, "Pause")
Else
_ShowTip("Program is paused", 0, 0)
GUICtrlSetData($Button2, "Run")
EndIf
EndFunc ;==>_Pause
Func _Count()
If _CheckIfPaused() Then Return ; Checks if the fPaused flag is set, and returns if so
$fCounting = Not $fCounting
If Not $fCounting Then
_ShowTip("Counting stopped")
EndIf
EndFunc ;==>_Count
Func _Exit()
; Uncomment if you want the user to be able to exit if the script is paused
;~ If _CheckIfPaused() Then Return
$fRunning = False
EndFunc ;==>_Exit
Func _CheckIfPaused()
If $fPaused Then
_ShowTip("", 0, 0)
ToolTip("You can't run script while the program is paused", 0, 0)
Return True
EndIf
Return False
EndFunc ;==>_CheckIfPaused
Func _ShowTip($sText, $iX, $iY, $iTime = 2000)
ToolTip($sText, $iX, $iY)
AdlibRegister("_CloseTips", $iTime)
EndFunc ;==>_ShowTip
Func _CloseTips()
AdlibUnRegister("_CloseTips")
ToolTip("")
EndFunc ;==>_CloseTips
If you want me to explain any line in the above code, then please do so by commenting.