But in the second code (see here below), I nested the function, and it doesn't work. Form is displayed without waiting. Why?!
Note the first line in the Resumable subs tutorial that
@Erel linked to in post #10:
Remember that that a call to Sleep or Wait For in a resumable sub
causes the code flow to return to the parent.
In your second example you are not waiting for SendRequest to complete in AppStart, so as soon as the code hits ' Wait For(PostRequest)...' it returns to the AppStart sub, loads the layout, and shows MainForm.
If you want to wait all the way through, you need to call all resumable subs in the chain with Wait For, right back to the point at which the first resumable sub is called (anything with a call to Sleep or Wait For is a resumable sub).
#Region Project Attributes
#MainFormWidth: 600
#MainFormHeight: 600
#End Region
Sub Process_Globals
Private fx As JFX
Private MainForm As Form
Private xui As XUI
Private Button1 As B4XView
End Sub
Sub AppStart (Form1 As Form, Args() As String)
Wait For(SendRequest) Complete (Result As Int)
MainForm = Form1
MainForm.RootPane.LoadLayout("Layout1")
MainForm.Show
End Sub
Sub Button1_Click
xui.MsgboxAsync("Hello World!", "B4X")
End Sub
Private Sub SendRequest() As ResumableSub
Wait For(PostRequest) Complete (Result As Int)
Return Result
End Sub
Private Sub PostRequest() As ResumableSub
Dim j As HttpJob
j.Initialize("",Me)
j.PostString("https://example.com", "example")
Wait for (j) JobDone(j As HttpJob)
If j.Success Then
Log(j.GetString)
End If
j.Release
Return 0
End Sub
I don't think that you necessarily have to return a value from SendRequest, but I think it looks neater to do so if there's something that could be returned, even if you're not going to use it.
See the first tip in the tutorial:
- If you don't need to return a value but still want to wait for the resumable sub to complete then return Null from the resumable sub and set the type in the calling sub to Object