B4J Question [solved] AppStart held by ResumableSub ?

svanneste

Member
Licensed User
Hi,
Please could you explain why using a ResumableSub from AppStart seems to hold the whole process ? I am sure I am doing something wrong but can't find why. Thanks

Hello world!!!
enters test1
enters subtest1
enters subsubtest1
enters test2
exits subsubtest1
exits test2
exits subtest1
exits test1
B4X:
Sub AppStart (Args() As String)
    pl(0,"Hello world!!!")
    test1
    test2
'    Wait For (test2) Complete(result As String)
    
    StartMessageLoop
End Sub

Sub test1 As ResumableSub
    pl(1,"test1")
    Wait For (subtest1) Complete(result As String)
    Sleep(0)
    pl(2,"test1")
    Return Null
End Sub

Sub subtest1 As ResumableSub
    pl(1,"subtest1")
    Wait For(subsubtest1) Complete(result As String)
    pl(2,"subtest1")
    Return Null
End Sub

Sub test2 As ResumableSub
    pl(1,"test2")
    Sleep(0)
    pl(2,"test2")
    Return Null
End Sub

Sub subsubtest1 As ResumableSub
    pl(1,"subsubtest1")
    Sleep(0)
    pl(2,"subsubtest1")
    Return Null
End Sub




Sub pl(rank As Int,what As String)
    Dim output As String
    Select rank
        Case 1
            output="enters "
        Case 2
            output="exits "
    End Select
    Log($"${output}${what}"$)
End Sub

While
Hello world!!!
enters test1
enters subtest1
enters subsubtest1
enters test2
B4X:
Sub AppStart (Args() As String)
    pl(0,"Hello world!!!")
    test1
'    test2
    Wait For (test2) Complete(result As String)
    
    StartMessageLoop
End Sub

Sub test1 As ResumableSub
    pl(1,"test1")
    Wait For (subtest1) Complete(result As String)
    Sleep(0)
    pl(2,"test1")
    Return Null
End Sub

Sub subtest1 As ResumableSub
    pl(1,"subtest1")
    Wait For(subsubtest1) Complete(result As String)
    pl(2,"subtest1")
    Return Null
End Sub

Sub test2 As ResumableSub
    pl(1,"test2")
    Sleep(0)
    pl(2,"test2")
    Return Null
End Sub

Sub subsubtest1 As ResumableSub
    pl(1,"subsubtest1")
    Sleep(0)
    pl(2,"subsubtest1")
    Return Null
End Sub




Sub pl(rank As Int,what As String)
    Dim output As String
    Select rank
        Case 1
            output="enters "
        Case 2
            output="exits "
    End Select
    Log($"${output}${what}"$)
End Sub
 

svanneste

Member
Licensed User
Thank for your reply. But it seems I can not move "StartMessageLoop" before a call to the WaitFor. So the only solution is to use a sub to reach the AppStart's end, you mean ? In some example (can't find it at the moment, Erel wrote, I've already seen the use of WaitFor in the AppStart sub).
That's not a problem to use another sub (it even makes the code clearer) but as I am learning B4X, that's the reason why I have asked. Thanks a lot again.

EDIT : perhaps the example I've seen was an UI app. I have to find it.
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
First rule of resumable subs: a call to Wait For or Sleep is equivalent to a call to Return from the calling sub perspective.

B4X:
Sub AppStart (Args() As String)
    pl(0,"Hello world!!!")
    test1
    Wait For (test2) Complete(result As String)
    
    StartMessageLoop
End Sub
As there is no message loop in non-ui apps, the above program will end when the Wait For line is reached.

Relevant thread: https://www.b4x.com/android/forum/t...t-for-sleep-in-server-handlers.81833/#content
 
Upvote 0
Top