Android Question Enhanced Wait For

agraham

Expert
Licensed User
Longtime User
The 'Sleep' and 'Wait For' are great facilities but I am now finding that I need to use Javascript style completion callbacks to process results from a Sub that uses one or other of them. I don't know if this suggestion would easily map onto the 'Wait For' mechanism but it would simplify the program flow if I could use
B4X:
Wait For PerformSearch
which would wait until Sub PerformSearch had properly returned.
B4X:
Sub btnPlace_Click   
   ...
   PerformSearch(PlaceList, "PlaceSearchComplete")
   ' Wait For PerformSearch ' I would like to do this
   ... ' presently I can't put completion code here or it runs too soon
End Sub

Sub PlaceSearchComplete
   Dim Status As String = "Found " & ResultList.Size & " Place"
   If ResultList.Size <> 1 Then Status = Status & "s"
   SetStatus(Status, Colors.Black)
End Sub

Sub PerformSearch(Data As List, Callback As String)
    If edtSearch.Text = "" Then
       Msgbox2Async("No search term provided!", "Error", "OK", "", "", Null, True)
       Wait For Msgbox_Result(Result As Int)
       CallSub(Me, Callback)
       Return
   End If
   ... ' might do a Sleep or two here to update the UI
   CallSub(Me, Callback)
End Sub
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Watch the resumable subs video tutorial. There is a simple solution for this task.

B4X:
Sub btnPlace_Click  
   ...
   Wait For (PerformSearch(PlaceList, "PlaceSearchComplete")) Complete (Success As Boolean) 'can be other types as well
   'Do whatever you like here. It will run after PerformanceSearch has returned.
End Sub

Sub PerformSearch(Data As List) As ResumableSub
    If edtSearch.Text = "" Then
       Msgbox2Async("No search term provided!", "Error", "OK", "", "", Null, True)
       Wait For Msgbox_Result(Result As Int)
       Return True
   End If
   Sleep(1000)
   Return True
End Sub
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
Thanks yet again Erel, I should have known that you would have a solution already . I'll look at that video again, I have watched it but I think I missed understanding the ResumableSub type properly. I am too conditioned to the JavaScript way of doing things as I am porting my DroidScript (JavaScript) mapping project to B4A.
 
Upvote 0

MarkusR

Well-Known Member
Licensed User
Longtime User
your subs would also more independent & reusable if u give this as parameters:
B4X:
ResultList & edtSearch.Text
its also better to see what comes in and go out.
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
Watch the resumable subs video tutorial. There is a simple solution for this task.
There also seems to be an another simpler solution that I found in Klaus' B4X Beginners Guide B4xBasicLanguageV1_4.pdf at page 61 "4.6.4 Waiting for a resumable sub to complete"
B4X:
Sub FirstSub
   Log("FirstSub started")
   SecondSub
   Wait For SecondSub_Complete
   Log("FirstSub completed")
End Sub

Sub SecondSub
   Log("SecondSub started")
   Sleep(1000)
   Log("SecondSub completed")
   CallSubDelayed(Me, "SecondSub_Complete")
End Sub
This seems to work but I don't understand why as there isn't a "SecondSub_Complete" Sub defined. Is calling "SecondSub_Complete" a way of creating an apparent "Complete" event for "SecondSub" which is then intercepted by Wait For?
 
Upvote 0

MarcoRome

Expert
Licensed User
Longtime User
Hi @agraham

Try
B4X:
Sub FirstSub
    Log("FirstSub started")
    Wait For (SecondSub) Complete (Result As Boolean)
    Log("FirstSub completed")
End Sub

Sub SecondSub As ResumableSub
    Log("SecondSub started")
    Sleep(1000)
    Log("SecondSub completed")
    Return True
End Sub
 
Last edited:
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…