Android Question how to wait for subs to complete before going to another

Makumbi

Well-Known Member
Licensed User
Please help i have these subs listed in my app but my biggest challenge is i wanted to first process one sub when it is done then move to the next one etc but currently it can not please help me out

Note
when the first sub is executed it goes to another sub when it reaches this
B4X:
 Wait For (jt) JobDone(jt As HttpJob)

B4X:
checkregisterusers
        ValidateReg
        Notify
        Processevents
        accountprocess


B4X:
below is one of my subs that iam supposed to processs
Sub checkregisterusers 
    'CheckNetConnectionsnew
    If IsConnectedToInternet= True Then
        'cursor1 = Starter.SQL1.ExecQuery("SELECT code || phone AS Phone FROM Register")
        Dim SQLQry As String = "DELETE FROM Allacounts"
        Starter.SQL1.ExecNonQuery(SQLQry)
        Dim SQLQry As String = "DELETE FROM student"
        Starter.SQL1.ExecNonQuery(SQLQry)
        Dim SQLQry As String = "DELETE FROM Balances"
        Starter.SQL1.ExecNonQuery(SQLQry)
        
        'Chr(0xF19D)
        'Starter.SQL1.ExecNonQuery("DROP TABLE IF EXISTS Balances")
        
        'Starter.SQL1.ExecNonQuery("CREATE TABLE Balances (Account Text,Names text, Class text,Stream text,Amount REAL, ID INTEGER)")
        cursor1 = Starter.SQL1.ExecQuery("SELECT phone AS Phone,Sex FROM Register")
        If cursor1.RowCount > 0 Then
            '    For i = 0 To cursor1.RowCount - 1
            cursor1.Position =0
            Dim Phoneq As String
            Phoneq = cursor1.Getstring("Phone")
            
            Dim sx As String
            sx = cursor1.Getstring("Sex")
            'Next
        End If
        Dim CustID As String = Phoneq' Customer ID
        Dim jt As HttpJob
        jt.Initialize("", Me)
        jt.Download("http://kccug.com/KabojjaApp/HandlerVBRegistration.ashx?customerid=" & CustID & "&sx=" & sx)
        Sleep(6000)
        'jt.GetRequest.Timeout = 2000 ' 10 seconds
        Wait For (jt) JobDone(jt As HttpJob)
        If jt.Success Then ' if job is success (http status code 200)
            Dim RetVal As String
            RetVal = jt.GetString
            If jt.GetString = "[]" Then
                'MsgboxAsync("No Records to Upload Yet for: " & CustID ,"SMIS")
            '    Return
            
            Else
            
                Dim jpt As JSONParser
                jpt.Initialize(jt.GetString)
                '    Log(jpt) ' will pr
                Dim quotes As List = jpt.NextArray
                For Each quot As Map In quotes
                    Log("Account: " & quot.Get("Account"))
                    Log("Name: " & quot.Get("Name"))
                    Log("Category: " & quot.Get("Category"))
                    Log("Froms: " & quot.Get("Froms"))
                    Log("Tos: " & quot.Get("Tos"))
                    Log(DateTime.Date(JsonDateToTick( quot.Get("Froms"))))
                    Dim l As Long
                    DateTime.DateFormat = "dd/MM/yyyy" ' "1961-08-29"
                    Dim datestring As String = DateTime.Date(JsonDateToTick( quot.Get("Froms")))
                    l = DateTime.DateParse(datestring)
                    DateTime.DateFormat = "dd/MM/yyyy"
                    Log(DateTime.Date(l))
                
                    Log(DateTime.Date(JsonDateToTick( quot.Get("Tos"))))
                    Dim lt As Long
                    DateTime.DateFormat = "dd/MM/yyyy" ' "1961-08-29"
                    Dim datestring As String = DateTime.Date(JsonDateToTick( quot.Get("Tos")))
                    lt = DateTime.DateParse(datestring)
                    DateTime.DateFormat = "dd/MM/yyyy"
                    Log(DateTime.Date(lt))
                    cursor1 = Starter.SQL1.ExecQuery("SELECT ID FROM Allacounts")
                    'cursor1 = SQL1.ExecQuery("SELECT Phone FROM Register")
                    If cursor1.RowCount > 0 Then
                        For i = 0 To cursor1.RowCount - 1
                            cursor1.Position = i
                
                            Dim NewID As Int
                            NewID = cursor1.GetInt("ID")
                        Next

                    End If
            
                    NewID = NewID +1      ' add 1 to the ID number to make a new ID field
    
                    Starter.SQL1.ExecNonQuery("INSERT INTO Allacounts VALUES('" & quot.Get("Account") & "','" & CustID  & "','" & NewID & "')")
                    'End If
                    'Grab the last ID number which is the highest number
                    
                    If quot.Get("Category")="Expired" Then
                        MsgboxAsync("Registration of " & quot.Get("Name") &" has Already Expired it was Valid from: " & DateTime.Date(l) &" To:"& DateTime.Date(lt) &" Please Pay 60000/= for Ayear Or 20000/= Per Term Via Mobile Money On 0782911364 Or 0702822227 To Activate" ,"SMIS")
                        Return
                        
                        
                    Else
                        If quot.Get("Category")="Inactive" Then
                            MsgboxAsync("Your Account is Currently Inactive Please Call: +256782911364 Or +256702822227 to Activate Thank you" ,"SMIS")
        
                            Return
                        End If
                        Log(RetVal) ' will print in log value returned from the server
                        'Msgbox("Records Not Processed. Please check Internet Connectivity and Try Try Again ","No Records Returned")
                        'Return
            
                        '    Dim jty As HttpJob
                        'jty.Initialize("", Me)
                        'jty.Download("http://kccug.com/KabojjaApp/HandlerVBStartedclear.ashx?customerid=" & CustID)
                
                    End If
                
                    
    
                
    
                Next
            Return
            End If
        End If
        'jt.Release
    Else
        MsgboxAsync("Error Connecting to the Server Please Check Your Internet Connection Or Load Mbs","Server Error")
        
        't1.Enabled = False
        Return
    End If
End Sub
 

Peter Simpson

Expert
Licensed User
Longtime User
Try something like this

B4X:
Wait For (checkregisterusers) Complete (Completed as Object)
Wait For (ValidateReg) Complete (Completed as Object)
Wait For (Notify) Complete (Completed as Object)
Wait For (Processevents) Complete (Completed as Object)
Wait For (accountprocess) Complete (Completed as Object)

At the Bottom of each sub add

B4X:
Return Null
 
Upvote 0

Makumbi

Well-Known Member
Licensed User
Thanks for the support but after trying
B4X:
Wait For (checkregisterusers) Complete (Completed as Object)
iam getting error Return type( in Sub signature) should be set explicitly and when i try to add
B4X:
Return Null
at the bottom of the sub
B4X:
checkregisterusers
it gives me error
B4X:
can not return values from this sub
 
Upvote 0

RB Smissaert

Well-Known Member
Licensed User
Longtime User
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…