Other [B4X] Quiz: Resumable Subs

Erel

B4X founder
Staff member
Licensed User
Longtime User
1. What is the difference between:
B4X:
Sub Activity_Create(FirstTime As Boolean)
   For Each link As String In Array("https://www.google.com", "https://www.bing.com", "https://www.duckduckgo.com")
     Download(link)
   Next
End Sub

Sub Download(link As String)
   Dim j As HttpJob
   j.Initialize("", Me)
   j.Download(link)
   Wait For (j) JobDone (j As HttpJob)
   If j.Success Then
     Log(j.GetString)
   End If
   j.Release
End Sub

And:
B4X:
Sub Activity_Create(FirstTime As Boolean)
   For Each link As String In Array("https://www.google.com", "https://www.bing.com", "https://www.duckduckgo.com")
     Dim j As HttpJob
     j.Initialize("", Me)
     j.Download(link)
     Wait For (j) JobDone (j As HttpJob)
     If j.Success Then
       Log(j.GetString)
     End If
     j.Release
   Next
End Sub




2. Code:
B4X:
Sub Activity_Create(FirstTime As Boolean)
   Counter(1)
End Sub

Sub Counter(i As Int)
   Log(i)
   Sleep(1000)
   Counter(i + 1)
End Sub

a. What will it print?
b. Will it cause a stack overflow?
c. What will happen if we rotate the device?

3. What is the output of:
B4X:
Sub Activity_Create(FirstTime As Boolean)
   SingleStepEachTime
   For i = 1 To 10
     Log("ActivityCreate: " & i)
     CallSubDelayed(Me, "SingleStepCanProceed")
     Wait For ActivityCreateCanProceed
   Next
   Log("ActivityCreate finished")
End Sub

Sub SingleStepEachTime
   For i = 1 To 10
     Log("SingleStepEachTime: " & i)
     Wait For SingleStepCanProceed
     CallSubDelayed(Me, "ActivityCreateCanProceed")
   Next
   Log("SingleStepEachTime finished")
End Sub

4.
B4X:
Sub Activity_Create(FirstTime As Boolean)
   Wait For Activity_Resume
   Log("Caught here!")
End Sub

Sub Activity_Resume
   Log("Can you print this one?")
End Sub
Which steps does the user need to do in order to print: "Can you print this one?" ?
 

MikeH

Well-Known Member
Licensed User
Longtime User
1. First example: Each URL will download in the order given. Second example: Each URL will download in order of their connection speed.

2a, 1, 2, 3... (each on a separate line)
2b. No? (Erel doesnt make code that crashes!)
2c. If rotate device, Activity_Create will start again, so the number restart at 1

3. Pass (Cant see Sub SingleStepCanProceed)

4. As run, output will be:
Can you print this one?
Caught here!
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
Is the sub counter (i as int) a resumable one?
The resumable symbol is not shown
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
1. First example: Each URL will download in the order given. Second example: Each URL will download in order of their connection speed.

2a, 1, 2, 3... (each on a separate line)
2b. No? (Erel doesnt make code that crashes!)
2c. If rotate device, Activity_Create will start again, so the number restart at 1

3. Pass (Cant see Sub SingleStepCanProceed)

4. As run, output will be:
Can you print this one?
Caught here!

1 - I agree With Mike
2a- I agree if the sub is a resumable one
2b- if 2a is a resumable sub, it will eventualy crash
2c- again, if resumable, it will continue counting. (Activity_pause->Activity_resume)
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Is the sub counter (i as int) a resumable one?
The resumable symbol is not shown
The forum doesn't show this symbol.

The correct answers are:
1. As Mike wrote, the first code will start all three downloads immediately while the second will only start the next download after the previous one completes.
Remember that Wait For and Sleep are equivalent to calling Return from the perspective of the calling sub (Activity_Create in this case).

2a. It is similar to a timer and it will indeed print 1, 2, 3, ...
b. No. This is again related to the equivalency of Sleep to Return. When Counter(1) calls Counter(2), 2 is printed and then when Sleep is called the code returns to the line after Counter(i + 1) and Counter(1) ends.
c. It will start counting from 1 again. The reason is that resumable subs waiting with Sleep are tied to the activity life cycle and are discarded when the activity is destroyed.

3. This is a nice example of how two resumable subs can "play ping pong" using Wait For.
The output is:
SingleStepEachTime: 1
ActivityCreate: 1
** Activity (main) Resume **
SingleStepEachTime: 2
ActivityCreate: 2
SingleStepEachTime: 3
ActivityCreate: 3
SingleStepEachTime: 4
ActivityCreate: 4
SingleStepEachTime: 5
ActivityCreate: 5
SingleStepEachTime: 6
ActivityCreate: 6
SingleStepEachTime: 7
ActivityCreate: 7
SingleStepEachTime: 8
ActivityCreate: 8
SingleStepEachTime: 9
ActivityCreate: 9
SingleStepEachTime: 10
ActivityCreate: 10
SingleStepEachTime finished
ActivityCreate finished

4.
B4X:
Sub Activity_Create(FirstTime As Boolean)
   Wait For Activity_Resume
   Log("Caught here!")
End Sub

Sub Activity_Resume
   Log("Can you print this one?")
End Sub

This one is still open. It should be quite simple.
The question is: Which steps does the user need to do in order to print: "Can you print this one?" ?
 
Upvote 0

udg

Expert
Licensed User
Longtime User
4. This one is still open.
Change orientation?
It looks similar to what we discussed in a thread in the B4J section.
Wait For in Activity Create consumes the Resume event that would naturally follow Create at program start (so no log "Can you.." printed). When user rotates its device the Activity_Resume sub will fire and the log message will appear.

Edit: No, I'm wrong. When the user rotates the device, Create is called again so a new WaitFor is fired. This means that the message is never showed. Right?

ps: will I have time during the next weekend to finally download newest versions of B4A and B4J? Time will tell..eheh
 
Last edited:
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Foreword: I admit I did not commit my only two neurons to study this new feature (damn old age :D)

I have only the feeling that it is very useful, probably its main purpose is to simulate multithreading.

However, I think that this thread is emblematic to prove the difficulty of understanding this topic, both for the few answers it has received and for the fact that quizzes have been created. That is why I think that to help late people like me, a more general explanation would be useful, which indicates not particulare use cases but general use cases (I can not explain this better).

Perhaps, however, this my consideration is due only to the fact that I have not read the relative thread properly.
 
Upvote 0

MikeH

Well-Known Member
Licensed User
Longtime User
4. Return to app from home screen
 
Upvote 0

inakigarm

Well-Known Member
Licensed User
Longtime User
Edited: Corrected bad reply
However, I think that this thread is emblematic to prove the difficulty of understanding this topic, both for the few answers it has received and for the fact that quizzes have been created.
Agree with you Lucas; I,ve read and tested this kind of subs in a several programs and (for me) it's a bit complex to understand yet (I thought about the questions and only was sure about first question)

After reading the explanations it's more clear now (or make sense)
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
4. Return to app from home screen
That's the correct answer. Press on the home key and then back to the app. This will cause the activity to be resumed without being killed. This time it will be intercepted by the sub as there is no Wait For waiting for this event.

Foreword: I admit I did not commit my only two neurons to study this new feature (damn old age :D)

I have only the feeling that it is very useful, probably its main purpose is to simulate multithreading.
This is not the purpose of this feature. The purpose of this feature is to make it simpler to manage the state when working with asynchronous tasks.
I'm afraid to say that your feedback is not relevant if you haven't bothered to make the minimum steps to learn the feature. Once you will use it you will see that it is simple.
You can start with the recommended templates:
[B4X] OkHttpUtils2 with Wait For
[URL='https://www.b4x.com/android/forum/threads/79532/#content'][B4X] SQL with Wait For
[/URL]
[URL='https://www.b4x.com/android/forum/threads/79532/#content'][URL='https://www.b4x.com/android/forum/threads/79578/#content']DoEvents deprecated and async dialogs (msgbox) [/URL][/URL]
 
Upvote 0

udg

Expert
Licensed User
Longtime User
Press on the home key and then back to the app
Unless the OS kills the app in the meanwhile..
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
I'm afraid to say that your feedback is not relevant if you haven't bothered to make the minimum steps to learn the feature.
I'm almost sure that this is not my case only and I think that this confirms my "suspicion":
this thread is emblematic to prove the difficulty of understanding this topic, both for the few answers it has received and for the fact that quizzes have been created

Anyway, I'm trying to understand :)
 
Upvote 0
Top