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: Copy
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.
 
Not a bug. You need a message loop for Wait For to work.
 
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!
 
Call a Sub that will be the real begin of your app.
B4X: Copy
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
 
Call a Sub that will be the real begin of your app.
B4X: Copy
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.
 

Similar Threads

Replies
14
Views
13K
  • Locked
  • Article Article
Android Tutorial [B4X] SQL with Wait For
Replies
16
Views
43K
Replies
36
Views
137K
Replies
67
Views
138K
Back
Top