Android Code Snippet [B4X] Wait for multiple tasks to complete

LucaMs

Expert
Licensed User
Longtime User
I, on the other hand, hope it was a joke, to indicate in any case an infinitesimal time, absolutely irrelevant.
 

Alessandro71

Well-Known Member
Licensed User
Longtime User
This whole debate about "status" seems to confirm my objection about "hack" and "language-level solution"
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Editing this post a bit:
The Wait For feature is one of the most important features of B4X programming language. Under the hood, it is one of the most complicated features in B4X, and still the API itself is quite clean and elegant.
Making it more complicated in order to solve an edge case is unreasonable.
As you can see in this simple code, we can already do it with a small loop.

And remember that in a real app, the logic can be more complicated than simply waiting for X tasks to complete. The suggested code is only a guidance for developers which can take it and modify it to meet their app requirements.
 
Last edited:

emexes

Expert
Licensed User

What are your thoughts about reframing it as:

B4X:
Dim NumStillRunning(1) As Int
TestX(NumStillRunning)
TestY(NumStillRunning)
TestZ(NumStillRunning)
Do While NumStillRunning(0) > 0
    Sleep(50)
Loop
Log("all tasks completed. Continue with your next task...")

Private Sub TestX (NumStillRunning() As Int)
    NumStillRunning(0) = NumStillRunning(0) + 1
    Sleep(Rnd(1, 1000))
    Log("TestX completed")
    NumStillRunning(0) = NumStillRunning(0) - 1
End Sub

Private Sub TestY (NumStillRunning() As Int)
    NumStillRunning(0) = NumStillRunning(0) + 1
    Sleep(Rnd(1, 1000))
    Log("TestY completed")
    NumStillRunning(0) = NumStillRunning(0) - 1
End Sub

Private Sub TestZ (NumStillRunning() As Int)
    NumStillRunning(0) = NumStillRunning(0) + 1
    Sleep(Rnd(1, 1000))
    Log("TestZ completed")
    NumStillRunning(0) = NumStillRunning(0) - 1
End Sub
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…