Android Question Code in event doesn't seem to run when Wait For is used

Andrew Gray

Member
Licensed User
Longtime User
I have code like the following. The Wait For part works fine, and does indeed resume when the button is clicked. But the code in Button_Click never runs (ClickedButton, which a global variable, thus remains empty).

B4X:
Sub MainModule
    
    Do While True
        Wait For Button_Click
        Log("You clicked " & ClickedButton)
    Loop
    
End Sub

Sub Button_Click

    Dim btn As Button = Sender
    ClickedButton = btn.Text

End Sub

Does Wait For somehow consume the button click event? And how can I fix this?

(This is to help a high school student with a coding exercise so I don't need a best practice solution, I just want a solution that is simple and works!)
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Does Wait For somehow consume the button click event?
Yes. It handles the event.

And how can I fix this?
In most cases you don't need the event sub at all, however if it is important for you then call it yourself:
B4X:
Sub MainModule
   
    Do While True
        Wait For Button_Click
        Log("You clicked " & ClickedButton)
        Button_Click '<----------------------------
    Loop
   
End Sub

Sub Button_Click

    Dim btn As B4XView = Sender
    ClickedButton = btn.Text

End Sub
 
Upvote 0

Andrew Gray

Member
Licensed User
Longtime User

Thanks. However, if Wait For consumes the event, the flag setting code would still not run.

In the end I managed to get the program working by using Sleep rather than Wait For...

B4X:
Sub Button_Click
    Dim btn As Button = Sender
    ClickedWord = btn.Text
End Sub

Sub MainModule
    
    Do While True
        Do While ClickedWord = ""
            Sleep(100)
        Loop
        Log("You clicked " & ClickedWord)
        ClickedWord = ""
    Loop
    
End Sub
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…