B4J Question ResumableSub in Non-UI app

Jorge M A

Well-Known Member
Licensed User
Obviously I'm trying to learn about B4J.
How do you use ResumableSub in a Non-UI app?
This is the code that doesn't work for me:

B4X:
'Non-UI application (console / server application)
#Region Project Attributes
    #CommandLineArgs:
    #MergeLibraries: True
#End Region

Sub Process_Globals
  
End Sub

Sub AppStart (Args() As String)
    Log("before")
    Wait For (Test) Complete (Finish As Boolean)
    Log("after")
    StartMessageLoop
End Sub

Sub Test As ResumableSub
    For i = 1 To 60
        Log(i)
        Sleep(20)    '<<-- Displaying Only First i=1
    Next
    Log("For/Next Finished")
    StopMessageLoop
    Return True
End Sub

'Return true to allow the default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
    Return True
End Sub
What is it that I don't understand?
Thank you.
 

Erel

Administrator
Staff member
Licensed User
What is it that I don't understand?
The first rule of resumable subs. From the calling method perspective, Wait For and Sleep are equivalent to Return. This means that the code returns on the second line and the program ends as there is no message queue.

Correct code:
B4X:
Sub AppStart (Args() As String)
    NewTest
    StartMessageLoop
End Sub

Sub NewTest
     Log("before")
    Wait For (Test) Complete (Finish As Boolean)
    Log("after")
End Sub
Sub Test As ResumableSub
    For i = 1 To 60
        Log(i)
        Sleep(20)    '<<-- Displaying Only First i=1
    Next
    Log("For/Next Finished")
    StopMessageLoop
    Return True
End Sub
 
Top