B4J Question Executing code after a resumable sub with a messageloop

MathiasM

Active Member
Licensed User
Hello

I think I'm losing it. I cannot understand what's wrong, but I'm pretty sure it's my lack of fully understanding the MessageLoop in non-ui applications.
I have this code (simplified)

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

Sub Process_Globals
    Dim Classes As List
End Sub

Sub AppStart (Args() As String)
    Classes.Initialize
    GetClasses
    StartMessageLoop
    For Each c As SmartschoolClass In Classes
        Log(c.Name)
    Next
    StopMessageLoop
End Sub

Sub GetClasses()
    Dim job As HttpJob
    job.Initialize("", Me)
    ''Here I prep the job
    
    
    Wait For (job) JobDone(job As HttpJob)
    If job.Success Then
        Dim json As JSONParser = ExtractJsonFromSmartschoolResponse(job.GetString)
        Dim root As List = json.NextArray
        
        For Each colroot As Map In root
            newClass.Initialize(colroot.Get("name"), colroot.Get("code"))
            Classes.Add(newClass)
            
        Next
        
    End If
End Sub

What I expect: The code runs, newClass objects are added to the Classes list.
After the GetClasses sub is run, I simply print the names of the classes in a for each loop.

But what happens:

The for each loop is never reached, the newClass objects are succesfully added in the Classes list. I can see that with the debugger.

If I set a breakpoint at line 15, the debugger never shows it reaches that line.

What am I doing wrong here?

Thanks!
 

EnriqueGonzalez

Well-Known Member
Licensed User
Longtime User
Bad idea to call code after startmessageloop.

Just create another function that holds that for block and call it in the end of the getclasses sub

On this new sub call stopmessageloop
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Several mistakes in your code.

Correct code:
B4X:
Sub Process_Globals
    Private Classes As List
End Sub

Sub AppStart (Args() As String)
   StartHere
    StartMessageLoop
   
 
End Sub

Sub StartHere
Classes.Initialize
    Wait For (GetClasses) Complete (Unused As Boolean)
 For Each c As SmartschoolClass In Classes
        Log(c.Name)
    Next
    StopMessageLoop
End Sub

Sub GetClasses() As ResumableSub
    Dim job As HttpJob
    job.Initialize("", Me)
    ''Here I prep the job
    
    
    Wait For (job) JobDone(job As HttpJob)
    If job.Success Then
        Dim json As JSONParser = ExtractJsonFromSmartschoolResponse(job.GetString)
        Dim root As List = json.NextArray
        
        For Each colroot As Map In root
            newClass.Initialize(colroot.Get("name"), colroot.Get("code"))
            Classes.Add(newClass)
            
        Next
        
    End If
  Return True
End Sub

Worth watching the video tutorial: https://www.b4x.com/android/forum/threads/b4x-resumable-subs-sleep-wait-for.78601/
 
Upvote 0
Top