Is there a way to wait for multiple ResumableSubs at the same time?
My problem is that I don't know which ResumableSub is going to finish first, so I don't know what order to wait for them in.
I did think of using global variables, but I figured that the .Completed method was there for a reason, and the only reason I could think of was to let you know that the result was ready.
As I understand it: if a ResumableSub completes and returns a value, and there's nothing waiting to catch the message, then the result is lost - is this correct?
Dim result As List
result.Initialize
For i = 1 To 10
SlowSub(i, result, 10)
Next
Wait For ResultsAvailable
Log(result)
Private Sub SlowSub (input As Int, result As List, ExpectedNumberOfResults As Int)
Sleep(Rnd(1, 1000))
result.Add(input * input)
If result.Size = ExpectedNumberOfResults Then
CallSubDelayed(Me, "ResultsAvailable")
End If
End Sub
I like this one better:
B4X:
Dim result As List
result.Initialize
For i = 1 To 10
SlowSub(i, result)
Next
Do While result.Size < 10
Sleep(50)
Loop
Log(result)
Private Sub SlowSub (input As Int, result As List)
Sleep(Rnd(1, 1000))
result.Add(input * input)
End Sub
The second one looks like a busy loop but it isn't. Its performance will be fine and it is actually more robust and safer (you will not get problems if you call SlowSub from some other place at the same time).