Bug? Wait For not work as in Documentation

milen_prg

New Member
I use B4J Console (non-UI) project:
I'm try the example from the documentation "B4X Language" about "Waiting for a resumable sub to complete":

B4X:
Sub Process_Globals
End Sub

Sub AppStart (Args() As String)
    Prb
    Log("after Prb 1")
    Wait For Prb_Complete
    Log("after Prb 2")
End Sub


Sub Prb
    Log("from Prb 1")
    Sleep(1000)
    Log("from Prb 2")
    CallSubDelayed(Me, "Prb_Complete")
End Sub

The expected result was Log:
from Prb 1
after Prb 1
from Prb 2
after Prb 2

But real is:
from Prb 1
after Prb 1

Wait For not work with risen event.
 

agraham

Expert
Licensed User
Longtime User
Not a bug. You need a message loop for Wait For to work.
 

milen_prg

New Member
Are you sure that the example is for console apps? You must have a StartMessageLoop in a console app and you cannot use Wait For or Sleep inside AppStart (in console app).
Yes, I start File>New>Console (Non-UI) Project
In the Documentation (B4X Language V2_4, page 68) is not mentioned nothing about StartMessageLoop. And how to use resumable subs, to call sub from AppStart, which calls resumable sub? Where I can read more about how this work?

Thank, you, for fast replies!
 

Sagenut

Expert
Licensed User
Longtime User
Call a Sub that will be the real begin of your app.
B4X:
Sub Process_Globals
End Sub

Sub AppStart (Args() As String)
    StartWait
    StartMessageLoop
End Sub

Private Sub StartWait
    Prb
    Log("after Prb 1")
    Wait For Prb_Complete
    Log("after Prb 2")
End Sub

Private Sub Prb
    Log("from Prb 1")
    Sleep(1000)
    Log("from Prb 2")
    CallSubDelayed(Me, "Prb_Complete")
End Sub
 

milen_prg

New Member
Call a Sub that will be the real begin of your app.
B4X:
Sub Process_Globals
End Sub

Sub AppStart (Args() As String)
    StartWait
    StartMessageLoop
End Sub

Private Sub StartWait
    Prb
    Log("after Prb 1")
    Wait For Prb_Complete
    Log("after Prb 2")
End Sub

Private Sub Prb
    Log("from Prb 1")
    Sleep(1000)
    Log("from Prb 2")
    CallSubDelayed(Me, "Prb_Complete")
End Sub
Thank, you, very much! I try this, its works and I understand the asynchronical programming in B4J. It is far more simple and logically clear than in Python, I like it!
P.S. I put StopMessageLoop at the end of the StartWait Sub, because, in otherwise, the IDE after the program end shown message something that the loop ends without start, but now all works just fine.
 
Top