I thought i would share my solution for a common problem that developers encounter when the need arises to have the program stop and wait for a task to complete before moving on to the next. It could come in handy to someone.
In these threads :
www.b4x.com
www.b4x.com
Erel explains how to wait for a task to complete before moving to the next task.
In my specific case this code could not be applied 'as is' . My app would crash every time . So here is my simple solution, ( not elegant , but works like a charm for me )
Create a Sub ( I call it the waiting sub ) which does nothing but keeps getting called as long as status (0) = "busy" . This keeps the code in a loop until the job is finished and status(0) = " "
My app is a card game where the player choses a hand of five cards form a grid of 20 cards. The cards are clicked or taped on one by one, and once the fifth card is selected the hand is evaluated and rated by the code, and the selected cards are removed from the grid . This only takes a second or two , but if the player were to start selecting a new hand before the eveluation process was done , the selected cards would be regularly highlighted as is expected , but ignored by the code that counts how many cards have been selected, sending the app into a tail spin which would eventually crash it. With the " Sleep comand the app kept crashing , and this solved it for me.
In these threads :
[B4X] Wait for multiple tasks to complete
A pattern such as this one can be used when you want to run multiple resumable subs concurrently and wait for all of them to complete before continuing: Dim status(1) As Int Test1(status) Test2(status) Test3(status) Do While status(0) < 3 Sleep(50) Loop...
[B4X] Wait For the first task to complete
This code snippet shows how to wait for several tasks to complete: https://www.b4x.com/android/forum/threads/b4x-wait-for-multiple-tasks-to-complete.150329/#content Here, we are waiting for the first event that is raised. This can be useful in cases where there are two different events, one for...
Erel explains how to wait for a task to complete before moving to the next task.
In my specific case this code could not be applied 'as is' . My app would crash every time . So here is my simple solution, ( not elegant , but works like a charm for me )
Create a Sub ( I call it the waiting sub ) which does nothing but keeps getting called as long as status (0) = "busy" . This keeps the code in a loop until the job is finished and status(0) = " "
My app is a card game where the player choses a hand of five cards form a grid of 20 cards. The cards are clicked or taped on one by one, and once the fifth card is selected the hand is evaluated and rated by the code, and the selected cards are removed from the grid . This only takes a second or two , but if the player were to start selecting a new hand before the eveluation process was done , the selected cards would be regularly highlighted as is expected , but ignored by the code that counts how many cards have been selected, sending the app into a tail spin which would eventually crash it. With the " Sleep comand the app kept crashing , and this solved it for me.
B4X:
Dim status(1) as String
Sub IV1_MouseClicked(EventData As MouseEvent)
' DiscardLeft = 100
If status(0) = "busy" Then
WaitingSub
End If
For i = 0 To 4
Splash(i).Visible =False
Next
Label1.Text = " "
etc. etc. etc.
End sub
Sub WaitingSub
If status(0) = "busy" Then
Return
End If
End Sub