B4J Question Wait For UI Event in AppStart

keirS

Well-Known Member
Licensed User
Longtime User
B4X:
Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private CBOTest As ComboBox
End Sub
Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("Test") 'Load the layout file.
    MainForm.Show
    CBOTest.Items.AddAll(Array("Item 1","Item 2","Item 3"))
    CBOTest.SelectedIndex = 1
    Wait For (CBOTest)  CBOTest_SelectedIndexChanged(Index As Int, Value As Object)
    Log("Wait Finished")
 
 
End Sub
'Return true to allow the default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
    Return True
End Sub
Sub CBOTest_ValueChanged (Value As Object)
 
End Sub
Sub CBOTest_SelectedIndexChanged(Index As Int, Value As Object)
    Log("Event Fired")
 
 
End Sub

From the code above the log output is:
B4X:
Waiting for debugger to connect...
Program started.
Wait Finished

I was expecting:

B4X:
Waiting for debugger to connect...
Program started.
Event Fired
Wait Finished

Taking out the Wait For I get:
B4X:
Waiting for debugger to connect...
Program started.
Event Fired

So something appears not to be working with Wait For.
 

DonManfred

Expert
Licensed User
Longtime User
So something appears not to be working with Wait For.
no.
move the event fired after the wait for line
B4X:
Wait For (CBOTest)  CBOTest_SelectedIndexChanged(Index As Int, Value As Object)
log($"Event fired after wait for with value ${Value}")
The eventsub is not needed if you use wait for.

it is however needed in the normal program-flow
 
Upvote 0

MarkusR

Well-Known Member
Licensed User
Longtime User
i would not do it in this way because it interupted AppStart
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
i would not do it in this way because it interupted AppStart
There is no problem with calling Wait For in AppStart. Wait For never blocks anything.

As DonManfred wrote, when an event is intercepted with Wait For, it doesn't raise the standard sub. It "steals" the event (once only).

You can call it yourself:
B4X:
  Wait For (CBOTest)  CBOTest_SelectedIndexChanged(Index As Int, Value As Object)
CBOTest_SelectedIndexChanged(Index, Value)
    Log("Wait Finished")
 
Upvote 0

MarkusR

Well-Known Member
Licensed User
Longtime User
with interuppted i meant it is ugly in design.
app logic or flow.
after appstart sub the app is started. the meaning of the name.
the issue is if you put everything into one sub u can not use parts of the functionality again.
as example if u will wait again if the user make this selection u can't call the sub again because it also show the form etc.
if often results in copy/paste of code blocks which is then bad for maintenance.

typical segmentation
AppStart
UserInput
ValidateInput
ProcessInput
 
Last edited:
Upvote 0
Top