Android Tutorial [B4X] Resumable subs that return values (ResumableSub)

Remember that that a call to Sleep or Wait For in a resumable sub causes the code flow to return to the parent.

Example:
B4X:
Sub Button1_Click
Sum(1, 2)
Log("after sum")
End Sub

Sub Sum(a As Int, b As Int)
Sleep(100) 'this will cause the code flow to return to the parent
Log(a + b)
End Sub
Output:
after sum
3

This is the reason why it is not possible to simply return a value.

Solution

Resumable subs can return a new type named ResumableSub. Other subs can use this value to wait for the sub to complete and get the desired return value.
B4X:
Sub Button1_Click
   Wait For(Sum(1, 2)) Complete (Result As Int)
   Log("result: " & Result)
   Log("after sum")
End Sub

Sub Sum(a As Int, b As Int) As ResumableSub
   Sleep(100)
   Log(a + b)
   Return a + b
End Sub

Output:
3
result: 3
after sum

The above Button1_Click code is equivalent to:
B4X:
Sub Button1_Click
   Dim rs As ResumableSub = Sum(1, 2)
   Wait For(rs) Complete (Result As Int)
   Log("result: " & Result)
   Log("after sum")
End Sub

The steps required are:

1. Add As ResumableSub to the resumable sub signature.
2. Call Return with the value you like to return.
3. In the calling sub, call the resumable sub with Wait For (<sub here>) Complete (Result As <matching type>)

Notes & Tips

- If you don't need to return a value but still want to wait for the resumable sub to complete then return Null from the resumable sub and set the type in the calling sub to Object.
- Multiple subs can safely call the resumable sub. The complete event will reach the correct parent.
- You can wait for resumable subs in other modules (in B4A it is relevant for classes only).
- The Result parameter name can be changed.
 
Last edited:

GMan

Well-Known Member
Licensed User
Longtime User
And it would be very nice if the Q posts the link to that new thread - otherwise we have to search again for (what topic ?) !
 

XbNnX_507

Active Member
Licensed User
Longtime User
Hi,
Is it ok to use
B4X:
Wait For(Sum(1, 2)) Complete '<-  wait to complete only, don't care about result..
if i'm not interested in the result?
B4X IDE doesn't throw any warnings.
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…