O Oliver Simith Member Oct 6, 2022 #1 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: Code: Copy '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: Code: Copy 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. Code: Copy 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!
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: Code: Copy '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: Code: Copy 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. Code: Copy 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 Oct 6, 2022 #2 B4X: Copy 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
B4X: Copy 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
Erel B4X founder Staff member Licensed User Longtime User Oct 6, 2022 #3 I recommend watching the resumable subs video tutorial: https://www.b4x.com/etp.html It explains all of this. Upvote 0
I recommend watching the resumable subs video tutorial: https://www.b4x.com/etp.html It explains all of this.
O Oliver Simith Member Oct 7, 2022 #4 Erel said: I recommend watching the resumable subs video tutorial: https://www.b4x.com/etp.html It explains all of this. Click to expand... 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. Code: Copy 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". Code: Copy 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
Erel said: I recommend watching the resumable subs video tutorial: https://www.b4x.com/etp.html It explains all of this. Click to expand... 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. Code: Copy 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". Code: Copy 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?
Erel B4X founder Staff member Licensed User Longtime User Oct 9, 2022 #5 1. Don't use activities. Switch to B4XPages. 2. p2 is not an event so there is no meaning to Wait For p2. If you want to wait for a resumable sub to complete then make it return a ResumableSub as explained here: https://www.b4x.com/android/forum/t...hat-return-values-resumablesub.82670/#content Upvote 0
1. Don't use activities. Switch to B4XPages. 2. p2 is not an event so there is no meaning to Wait For p2. If you want to wait for a resumable sub to complete then make it return a ResumableSub as explained here: https://www.b4x.com/android/forum/t...hat-return-values-resumablesub.82670/#content