B4J Question Can you Wait For a resumable sub that contains a Wait For in it?

aminoacid

Active Member
Licensed User
Longtime User
I am curious as to why this does not work.

I am calling the following Resumable Sub in a Console Application. As you can see, it contains a "Wait For" to wait for a TCP connection to be established:

B4X:
Sub ConnectToWLIP As ResumableSub
    Private Wclient As Socket
    If Wclient.IsInitialized Then Wclient.Close
    Wclient.Initialize("WClient")
    Log("Connecting to WLIP")
    Wclient.Connect("192.168.1.123", 2000, 10000)
    Wait For WClient_Connected (Successful As Boolean)
    If Successful Then
        AST2.Initialize(Wclient.InputStream, Wclient.OutputStream, "AST2")
        Log("WLIP Connected")
        Return True
    Else
        Log("WLIP Connected FAILED")
        Return False
    End If
End Sub

I need to wait for the TCP connection to be established before I proceed with doing other things. So, when I call the above sub with the following statements:

B4X:
            Wait For (ConnectToWLIP) Complete (success As Boolean)
            If success Then
                Log("Connected: Ready to Go")
            Else
                Log("NOT Connected: Do not proceed")
            End If

The Log output is only:

Connecting to WLIP

and then the program terminates. No other Log statements are executed and the program never returns to the Calling Wait For statement.

But if I call the above sub with the following statements:


B4X:
            ConnectToWLIP
            Log("Ready to Go")

It works, but obviously not the way I would like since it returns without waiting for the TCP connection to be established. Here is the Log output:

Ready to Go
WLIP Connected


Obviously, what I need is "Ready to Go" to come after "WLIP Connected".

and the program does NOT terminate abnormally like it did with the previous Call.

Am I missing something? I don't understand why the first call with the Wait For does not work since the logic seems to be okay.
 

agraham

Expert
Licensed User
Longtime User
Probably this

 
Upvote 1

aminoacid

Active Member
Licensed User
Longtime User
Probably this


That explains it! Thanks
 
Upvote 0
Top