I am making AutoIt code and one of the items on the GUI needs to be updated every few seconds, and I can seem to get it to do it. To make it simple I have written some code that shows the problem:
$num = 0
GUICreate("Example")
$Pic1 = GUICtrlCreateLabel($num, 10, 10)
GUISetState()
While 1
   sleep(1000)
   $num = $num + "1"
WEnd
If the code was working then the number would change, but it does not. How do I make it refresh?
The number is updating, but your data is not. Plus, you are mixing integers with strings.
Luckily AutoIt converts them automatically. But take care, because you will have problems in other programming languages.
Here you go:
Local $num = 0
Local $hGUI = GUICreate("Example")
Local $Pic1 = GUICtrlCreateLabel($num, 10, 10)
GUISetState()
Local $hTimer = TimerInit()
While 1
    ;sleep(1000)
    If TimerDiff($hTimer) > 1000 Then
        $num += 1
        GUICtrlSetData($Pic1, $num)
        $hTimer = TimerInit()
    EndIf
    If GUIGetMsg() = -3 Then ExitLoop
WEnd
P.S.: Avoid using sleep in these situations while they will pause your script.
This is easily done with AdLibRegister passing the function name and then 1000 milliseconds (1 second).
Local $num = 0
Local $hGUI = GUICreate("Example")
Local $Pic1 = GUICtrlCreateLabel($num, 10, 10)
Local $msg
GUISetState()
AdLibRegister("addOne", 1000)
While 1
    $msg = GUIGetMsg()
    Switch $msg
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
WEnd
Func addOne ()
    $num += 1
    GUICtrlSetData($Pic1, $num)
EndFunc
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With