Note the first line in the Resumable subs tutorial that @Erel linked to in post #10:But in the second code (see here below), I nested the function, and it doesn't work. Form is displayed without waiting. Why?!
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.Remember that that a call to Sleep or Wait For in a resumable sub causes the code flow to return to the parent.
I have a B4J UI application. But I need to do a few http requests/responses before the main form is displayed.Http requests are never executed on the main thread. We are no longer in the 90s
I am using Wait For.Making requests on the main thread is the wrong solution. It would have caused your app to freeze anywhere between a few seconds to a minute+.
You can do whatever you need, including showing the main form after the request completed, with Wait For.
This is an example.Don't call MainForm.Show before the response arrived.
#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)
PostRequest
MainForm = Form1
MainForm.RootPane.LoadLayout("Layout1")
MainForm.Show
End Sub
Sub Button1_Click
xui.MsgboxAsync("Hello World!", "B4X")
End Sub
Private Sub PostRequest()
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
End 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)
PostRequest
MainForm = Form1
MainForm.RootPane.LoadLayout("Layout1")
End Sub
Sub Button1_Click
xui.MsgboxAsync("Hello World!", "B4X")
End Sub
Private Sub PostRequest()
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)
MainForm.Show '<------------
End If
j.Release
End Sub
This is it. Thank you.
So in the first code, seen here, it works as expected. The flow of the program is halted until function PostRequest is finishedThis is it. Thank you.
#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(PostRequest) 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 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
#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)
SendRequest
MainForm = Form1
MainForm.RootPane.LoadLayout("Layout1")
MainForm.Show
End Sub
Sub Button1_Click
xui.MsgboxAsync("Hello World!", "B4X")
End Sub
Private Sub SendRequest()
Wait For(PostRequest) Complete (Result As Int)
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
Note the first line in the Resumable subs tutorial that @Erel linked to in post #10:But in the second code (see here below), I nested the function, and it doesn't work. Form is displayed without waiting. Why?!
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.Remember that that a call to Sleep or Wait For in a resumable sub causes the code flow to return to the parent.
#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
- 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