I am trying to call three subs from a mainsub as a chain. It is important that every sub completes before the mainsub continues. So what's wrong with this example? I'm not sure about the Sleep(1000) will be safe enough, since each sub might take longer than the other. What I want to do is to download data with jOkHttpUtils2 from a MySQL database. Once all jobs are complete it may continue.
Somehow the ThirdSub is never logged, and either is Log("Completed").
B4X:
#Region Project Attributes
#MainFormWidth: 600
#MainFormHeight: 600
#End Region
Sub Process_Globals
Private fx As JFX
Private MainForm As Form
End Sub
Sub AppStart (Form1 As Form, Args() As String)
MainForm = Form1
'MainForm.RootPane.LoadLayout("Layout1") 'Load the layout file.
MainForm.Show
MainSub
End Sub
'Return true to allow the default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
Return True
End Sub
Sub MainSub
Log("MainSub started")
FirstSub
Wait For FirstSub_Complete
SecondSub
Wait For SecondSub_Complete
ThirdSub
Wait For ThirdSub_Complete
Log("Completed")
End Sub
Sub FirstSub
Log("FirstSub started")
SecondSub
Wait For SecondSub_Complete
Log("FirstSub completed")
End Sub
Sub SecondSub
Log("SecondSub started")
Sleep(1000)
Log("SecondSub completed")
CallSubDelayed(Me, "SecondSub_Complete")
End Sub
Sub ThirdSub
Log("ThirdSub started")
Sleep(2000)
Log("ThirdSub completed")
CallSubDelayed(Me, "ThirdSub_Complete")
End Sub
Somehow the ThirdSub is never logged, and either is Log("Completed").