B4J Question ResumableSub in Non-UI app

Discussion in 'B4J Questions' started by Jorge M A, Jun 11, 2019.

  1. Jorge M A

    Jorge M A Active 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:

    Code:
    '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 StringAs Boolean
        
    Return True
    End Sub
    What is it that I don't understand?
    Thank you.
     
  2. Erel

    Erel Administrator Staff Member Licensed User

    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:
    Code:
    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
     
    ShaneG30 and Jorge M A like this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice