Android Question Wait for task to complete

Polaris

Member
Licensed User
Longtime User
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 :


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
 
Top