Android Question [Solved] Waiting for two or more Resumable routines to complete

LucaMs

Expert
Licensed User
Longtime User
B4X:
obj.AnimationX ' <--- with some Sleeps inside (also, it would be "nice" if the class had also an AnimationCompleted event)
' The next line must be executed without waiting for the end of the animation.
Wait For (DifferentObject.DoSomething) Complete(Unused As Boolean)

How to do it?
 

LucaMs

Expert
Licensed User
Longtime User
I had forgotten what I wanted to achieve: the simultaneous execution of the two routines (AnimationX and DoSomething) and then the execution of the next line only when both were completed. If the second took less time than the first, the flow would continue before the animation was completed.

B4X:
obj.AnimationX ' <--- with some Sleeps inside -<< 10 seconds >>

Wait For (DifferentObject.DoSomething) Complete(Unused As Boolean) ' << 5 seconds >>

' I would like the next line to execute only upon completion of both methods.
' ...

(not knowing the two durations in advance)
 
Last edited:
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Maybe so (not tried, just wrote directly here 😁):
B4X:
Wait For (DoBoth) Complete(Fake As Boolean)


Sub DoBoth As Resumable
    obj.AnimationX '<< 10 seconds >>
    Wait For (DifferentObject.DoSomething) Complete(Unused As Boolean) ' << 5 seconds >>
    Return True
End Sub
 
Last edited:
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Just to say...

To test the code above I used B4J, very useful also for these things, thanks to the common language:

B4X:
Sub AppStart (Form1 As Form, Args() As String)
    Wait For (DoBoth) Complete(Fake As Boolean)
    Log("Done")
End Sub

Sub DoBoth As ResumableSub
    Sleep(500)
    Log("animation completed")
    Sleep(250)
    Log("second method completed")
    Return True
End Sub
 
Last edited:
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
The question is not clear. The question in the first post was answered.
Because the question in the first post was wrong, not what I needed. The problem is explained in my second post (#3) and better here, I hope.


B4X:
obj.AnimationX 
DifferentObject.DoSomething '<--- resumable routine
' Third line
I would like the third line of code to run only after the first two are completed, both are resumable routines.

To wait for one to complete, I can use Wait For but for two consecutive ones?
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
It means wanting to run two routines simultaneously, like using threads.
Since the resumables do not block the flow (nor do animations, for example SetLayoutAnimated) the two routines can be run as if they were in two different threads.

But how to wait for the completion of both, not knowing a priori their duration? If I knew for sure which of the two would take longer, I would launch them in order, using the Wait For calling the second, the one with longer duration.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Now it is clear.


B4X:
    Dim rs1 As ResumableSub = Res1
    Dim rs2 As ResumableSub = Res2
    Do Until rs1.Completed And rs2.Completed
        Sleep(50)
    Loop
    Log("after")


Sub Res1 As ResumableSub
    Sleep(Rnd(100, 4000))
    Log("Res1 completed")
    Return True
End Sub

Sub Res2 As ResumableSub
    Sleep(Rnd(100, 4000))
    Log("Res2 completed")
    Return True
End Sub
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Now how can I explain that I have to study your code because it is out of my "habits"?
This is my problem (to understand your code):
Dim rs1 As ResumableSub = Res1
"You" cannot declare an object as "some routine", of course, just as "some class".
But actually ResumableSub is a type / class:
B4X:
Sub ASDF As ResumableSub ' <---
🤔

When my usual cervical headache :mad: has passed, I promise I will try to understand :)
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
In most cases we use the returned ResumableSub without actually seeing the type:
B4X:
Wait For (res1) Complete (Result As Something)
'same as
Dim o As Object = res1
Wait For (o) Complete (Result As Something)
'same as
Dim rs As ResumableSub = res1
Wait For (rs) Complete (Result As Something)
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
I try to recap (the headache is still there :mad: ).

If a routine (sub) returns a value, it is a function.
Then we write/define the return type by the function:

Public Sub Circumference (Radius As Double) As Double

When we write a routine containing "Sleep" or "Wait For" it is considered/becomes a routine of type Resumable. The type of data that it can return (optionally) is ResumableSub:

Sub MyRoutine As ResumableSub
Sleep(100)

Thus, both the routine and the value returned by it are ResumableSub. Is the return type the routine "itself"? No, since we can return any type of data:
B4X:
Sub MyRoutine As ResumableSub
    Dim Result As Int
    '...
    Return Result
End Sub

In short, I believe that my doubts are that the Resumable signature, in this case, actually declares the routine type (although even without this it is implied, when it contains a Sleep or Wait For) instead of the type of data returned as if it was a function.



[I didn't use Code tags because I couldn't use bold]
 
Upvote 0
Top