Android Question Callsub with waiting

Scantech

Well-Known Member
Licensed User
I am interested if there is anything that deals with Callsub and wait for the sub is ended. Is there any examples of this?

I need to call a sub in Main from Service. If Main is paused it will still call it and trigger the sub and wait for the sub to end.

B4X:
CallSub(Main, "Testing")
'Need to wait here until the Testing Sub is Ended
 

DonManfred

Expert
Licensed User

Scantech

Well-Known Member
Licensed User
I assume that Main.Testing is a resumable sub. Otherwise you don't need to do anything special.

B4X:
Sub Testing As ResumableSub
 ...

'different module
Dim rs As ResumableSub = CallSub(Main, "Testing")
if rs.IsInitialized Then
 Wait For (rs) Complete (Result As Object)
 'after
Else
 Log("Activity was paused")
End If
I thought Anything after CallSub will have to wait for the sub to be completed? But when you use CallSubDelayed it will go to next code with out waiting?

Can this be used with CallSubDelayed. I want to unpause the Main and make the call and wait for completion.
 
Last edited:

Scantech

Well-Known Member
Licensed User
B4X:
Sub Service_Create
    CallSub(Main, "Testing")
    Log("After CallSub in Service")
    
End Sub
B4X:
Sub Testing
    For x = 0 To 20000
        Log("TESTING IN MAIN " & x)
    Next
End Sub
This works but i need to use Callsubdelayed to unpause the activity
 

Scantech

Well-Known Member
Licensed User
This is the difference with Erels Example.

'Erels Example above
B4X:
Sub Service_Create
    'different module
    Dim rs As ResumableSub = CallSub(Main, "Testing")
    If rs.IsInitialized Then
        Wait For (rs) Complete (Result As Object)
        Log("AFTER WAITING")
    Else
        Log("Activity was paused")
    End If
End Sub
B4X:
Sub Testing As ResumableSub
    Log("TESTING")
End Sub
'Result
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
*** Service (doing) Create ***
TESTING
** Service (doing) Start **
AFTER WAITING
** Activity (main) Pause, UserClosed = true **
** Service (starter) Destroy (ignored)**
** Service (doing) Destroy **

'This Example
B4X:
Sub Service_Create
    'different module
    CallSub(Main, "Testing")
    Log("AFTER WAITING")

End Sub
B4X:
Sub Testing As ResumableSub
    Log("TESTING")
End Sub
'Result
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
*** Service (doing) Create ***
TESTING
AFTER WAITING
** Service (doing) Start **
** Activity (main) Pause, UserClosed = true **
** Service (starter) Destroy (ignored)**
** Service (doing) Destroy **
Notice the difference with **Service(doing)Start**
 
Top