B4J Question StartMessageLoop was not called

mbenam

Member
Hi,

trying to learn "wait for" in a non ui app. I get the error "Program terminated (StartMessageLoop was not called)." Here is my code.

Non ui app:
Sub Process_Globals

End Sub

Sub AppStart (Args() As String)
    
    Wait For (getUrl) Complete (Result As Boolean)
    Log("Hello world!!!")
    StartMessageLoop
End Sub

Sub getUrl() As ResumableSub
    Dim client As HttpJob
    
    client.Initialize("",Me)
    client.Download("https://www.google.com")
    
    Wait For (client) JobDone (res As HttpJob)
    If res.Success=False Then
        Log("error")
        Return False
    End If
    Dim CookieContainer As String = res.Response.GetHeaders.Get("set-cookie")
    Log(CookieContainer)
    Return True
End Sub
 

EnriqueGonzalez

Expert
Licensed User
Longtime User
Scratch the wait for line.
Change it to just geturl

You are not using the url result, so then you don't have to return a value. Instead of returning just call stopmessageloop
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Remember the number #1 rule of resumable subs: a call to Wait For or Sleep is equivalent to a call to Return, from the calling sub perspective.
This means that the code flow will return at line 7 and the program will end, as there is no message loop.

This will work as you expect:
B4X:
Sub AppStart (Args() As String)
    MyStart
    StartMessageLoop
End Sub

Sub MyStart
   Wait For (getUrl) Complete (Result As Boolean)
     Log("Hello world!!!")
End Sub
 
Upvote 0

mbenam

Member
Erel,

I have been doing what you suggested in your code. But doing that it seems like I need to pile up all the code that I want to execute right after the httpjob within the MyStart sub. I wanted to avoid that and make codes modular. I know that I can call various subs within myStart. But if I have to do another httpjob later in the code, then I start the rabbit hole again. Not sure if I am making myself clear. I watched your video on resumable subs and thought the code I posted might work.

Thanks.
 
Upvote 0
Top