Android Question Need help with Wait For

Hi, all. I was trying the following codes to learn Wait For and Sleep. However, I always failed to get the output right.
The code is like this:
my code:
'main
sub activity_create
    p1
    p2
end sub

private sub p1
    log("p1 started")
    Wait For p2
    log("p1 ended") 'It seems that this line of code is always blocked by the Wait For command on the previous line
end sub

private sub p2
    log("p2 started")
    sleep(5000)
    log("p2 ended")
end sub

The output is:
output:
p1 started
-p2 started
-p2 ended

I expect "p1 ended" to be part of the output. However, The code after "Wait for p2" in subroutine p1 seems to have never been executed. When p2 is raised in Activity_create which means the requirement for resuming p1 is met, shouldn't subroutine p1 continue execution from where it is waiting?

If I change subroutine p2 to the following, just as the B4X Language Tutorial taught, then I will get the expected output.
modified subroutine p2:
Private Sub p2
    Log("-p2 started")
    Sleep(5000)
    Log("-p2 ended")
    CallSubDelayed(Me, "p2") ' When this line is added, I got the expected output
End Su

'output I expected
p1 started
-p2 started
-p2 ended
p1 ended

So, is an explicit command like CallSubDelayed a must for the machine to get back to where it is waited for( In this case, the WaitFor in subroutine p1)?
I think I must have missed the point somewhere. Please tell me where I have misunderstood and any comment is appreciated. Thanks!
 

agraham

Expert
Licensed User
Longtime User
B4X:
Sub Button1_Click
    p1
    p2
End Sub

private Sub p1
    Log("p1 started")
    Wait For (p2) Complete (result As Object)
    Log("p1 ended") 'It seems that this line of code is always blocked by the Wait For command on the previous line
End Sub

private Sub p2  As ResumableSub
    Log("p2 started")
    Sleep(5000)
    Log("p2 ended")
    Return True
End Sub

p1 started
p2 started
p2 started
p2 ended
p2 ended
p1 ended
 
Upvote 0
I recommend watching the resumable subs video tutorial: https://www.b4x.com/etp.html
It explains all of this.
Hi, Erel, Thanks for the link. following the video tutorial, I got the expected output. At the same time, I found something interesting and also confusing.

Successful Code Example:
Sub Activity_Create
    p1
End Sub

Sub p1
    log("p1 started")
    Wait For Button1_Click     ' Here, Button1_click instead of p2 is "Waited for"
    log("p1 ended")
End Sub

Sub p2
    log("p2 started")
    log("p2 ended")
End Sub

Sub Button1_Click
    p2     
End Sub

'output
p1 started
p1 ended
' p1 ended was in output which means p1 was resumed.

However, if I change Wait For Button1_Click to Wait For p2, then the program fails to output "p1 ended".

modified but failed code:
Sub Activity_Create
    p1
End Sub

Private Sub p1
    log("p1 started")
    Wait For p2         
    log("p1 ended")
End Sub

Private Sub p2
    log("p2 started")   
    log("p2 ended")
End Sub

Sub Button1_Click
    p2
End Sub

'output
p1 started
p2 started
p2 ended

In short, it seems there's a difference between Wait for Button1_Click and Wait For p2. Is that true?
 
Upvote 0
Top