Jump to content

Recommended Posts

Posted

Hi all,

I often disable a button as soon as its event starts, so the user can’t trigger it twice. This doesn’t always work. If the script is busy and the GUI doesn’t react right away, the user often clicks several times, and the event then runs once per click. (I tried to debug/catch why this happens, but unsure of the why). In my productive code I normally do:

$iMsg = GuiGetMsg()
    Switch $iMsg
        Case $idBtn
            _GuiDisable()
            _OnBtnFunc()
            _GuiEnable()

But there is also some registered message with GUIRegisterMsg, but on all code path there is an Return $GUI_RUNDEFMSG and only some logical evaluation, nothing should block the main loop in the script.

Here is a minimal example:

#NoTrayIcon
#Region Compiler Settings
    #Tidy_Parameters=/reel /sf /ri
    #AutoIt3Wrapper_UseX64=n
#EndRegion Compiler Settings

#Region Includes
    #include <GUIConstantsEx.au3>
    #include <WindowsConstants.au3>
#EndRegion Includes

#Region Options/Variables
    Opt("MustDeclareVars", 1)

    Global $g_bRun = True
    Global $g_idLblStep = 0
#EndRegion Options/Variables

#Region Main
    _Main_OnEvent()
    ;- _Main_GetMsg()

    Func _Main_GetMsg()
        Local $hGui = GUICreate("Button Debounce Test (MessageLoop)", 320, 140)
        Local $idBtn = GUICtrlCreateButton("Start", 20, 20, 280, 40)
        Local $idLblStep = GUICtrlCreateLabel("Ready", 20, 80, 280, 40)
        GUISetState(@SW_SHOW, $hGui)

        While 1
            Switch GUIGetMsg()
                Case $GUI_EVENT_CLOSE
                    ExitLoop
                Case $idBtn
                    _OnBtnClick($idBtn, $idLblStep)
            EndSwitch
        WEnd

        GUIDelete($hGui)
    EndFunc   ;==>_Main_GetMsg

    ; OnEvent variant (GUIOnEventMode)
    Func _Main_OnEvent()
        Opt("GUIOnEventMode", 1)

        Local $hGui = GUICreate("Button Debounce Test (OnEvent)", 320, 140)
        GUISetOnEvent($GUI_EVENT_CLOSE, "_OnClose")

        Local $idBtn = GUICtrlCreateButton("Start", 20, 20, 280, 40)
        GUICtrlSetOnEvent($idBtn, "_OnBtnClickEvent")

        $g_idLblStep = GUICtrlCreateLabel("Ready", 20, 80, 280, 40)
        GUISetState(@SW_SHOW, $hGui)

        While $g_bRun
            Sleep(50)
        WEnd

        GUIDelete($hGui)
    EndFunc   ;==>_Main_OnEvent
#EndRegion Main

#Region Functions
    Func _OnBtnClick($idBtn, $idLblStep)
        GUICtrlSetData($idLblStep, "Step 1: Waiting 1 second ...")
        Sleep(1000)

        GUICtrlSetData($idLblStep, "Step 2: Button disabled, waiting 2 seconds ...")
        GUICtrlSetState($idBtn, $GUI_DISABLE)
        Sleep(2000)

        GUICtrlSetState($idBtn, $GUI_ENABLE)
        GUICtrlSetData($idLblStep, "Step 3: Button enabled, done")
    EndFunc   ;==>_OnBtnClick

    ; OnEvent handler: control ID comes from @GUI_CtrlId
    Func _OnBtnClickEvent()
        _OnBtnClick(@GUI_CtrlId, $g_idLblStep)
    EndFunc   ;==>_OnBtnClickEvent

    Func _OnClose()
        $g_bRun = False
    EndFunc   ;==>_OnClose
#EndRegion Functions

To reprdouce: Click the button several times during the first second, before it gets disabled. Every click is handled one after the other once the handler returns, so the sequence runs again for each click.

I can use try to use a timer to debounce a button like this:

If $idBtnLast <> 0 And TimerDiff($idBtnLast) < 400 Then ContinueLoop
_OnBtnClick($idBtn, $idLblStep)
$idBtnLast = TimerInit()

But with complex GUI that might be little over the top. Is there something obvious that I am missing?

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
×
×
  • Create New...