B4J Question Having Trouble with Simple Wait For

GuyBooth

Active Member
Licensed User
Longtime User
I have no problems suing the Wait For (Sub) Complete (Result as Boolean).

In my current application I need to use a construct like this, which I am sure has worked for me in the past:
B4X:
Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private Button1 As Button
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("Layout1") 'Load the layout file.
    MainForm.Show
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 Button1_Click
    Log("Clicked")
    Dim SerialNo As String
    SerialNo = "15"
    SendIt_Back (SerialNo)
    Wait For CDInMB_Result (Found As Boolean)
    Log(Found)
End Sub

Sub SendIt_Back (SerialNo As String)
    Dim Found As Boolean
    Found = (SerialNo = "15")
    CallSub2(Me, "CDInMB_Result", Found)
End Sub

But now I am getting no response from the first click.
After the four clicks the logs show this:
B4X:
Clicked
Clicked
true
Clicked
true
Clicked
true

Can anyone see what am I doing wrong?
 

LucaMs

Expert
Licensed User
Longtime User
B4X:
Sub Button1_Click
   Log("Clicked")
   Dim SerialNo As String
   SerialNo = "15"
   Wait For (SendIt_Back(SerialNo)) Complete (Found As Boolean)
   Log(Found)
End Sub

Sub SendIt_Back (SerialNo As String) As ResumableSub
   Dim Found As Boolean
   Found = (SerialNo = "15")
   Return Found
End Sub
 
Upvote 0

GuyBooth

Active Member
Licensed User
Longtime User
Thanks for your response LucaMS.
Your code matches the construct I mention at the beginning of my post, which works, but which I cannot use in this particular context, because there are circumstances when no response will be sent.
 
Upvote 0

GuyBooth

Active Member
Licensed User
Longtime User
Ok, got it!
Must be a CallSubDelayed call, not CallSub. So SendIt_Back becomes:

B4X:
Sub SendIt_Back (SerialNo As String)
    Dim Found As Boolean
    Found = (SerialNo = "15")
    CallSubDelayed2(Me, "CDInMB_Result", Found)
End Sub

Sigh! So many things to remember ….
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
I reached the same solution right today for a my project.

Remembering it is easy, just think that with a simple Call the flow would return immediately after this call, while using CallSubDelayed the routine is completed and only then the Call is executed.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…