Android Question Wait For with OkHTTPUtils2 and CallSubDelayed

Alex_197

Active Member
Licensed User
Hi, I have a question.

I created a small project (see the attachment) where I tried to learn how to work with a subject.

I created a class where I put all the logic -the main procedure in this class calls a private sub that sends request to the server

Test Class:
Sub Class_Globals
    Public FacilityID As Int
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize
    
End Sub

Public Sub Sub1
    
    Try
        
        Dim CoJobs As Int=2
        
        Log("Sub1 Started for Facility " & FacilityID)
        
        'Let say I need to call CallJob1 for each co-jobs in cutomer's order
        
        For CoJob=1 To CoJobs
        
            CallJob1(CoJob)
            
            Log("CallJob1 Called  for CoJob " & CoJob)
            
            Wait For CallJob1_Complete
            
            Log("CallJob1 Completed for CoJob " & CoJob)
        
        Next
        
        Log("Sub1 Ended for Facility " & FacilityID)
        
    Catch
        Log(LastException)
    End Try
    
End Sub

Private Sub CallJob1(i As Int)
    
    Try
        Dim params As String
        Dim j As HttpJob
        Dim URL As String
        
        j.Initialize("",Me)
                
        params="Ticket=123&cojob=" & i
        
        URL="http://example.com"
        
        j.PostString(URL,params)
        
        wait For (j) JobDone(j As HttpJob)
        
        If j.Success Then
            Log("Response " & i)           
        Else
            Log(i & "Opps")
        End If
        
        j.Release

        
        CallSubDelayed(Me,"CallJob1_Complete")
            
    Catch
        Log(LastException)
    End Try
    
End Sub

I call this class from activity

main activity:
Sub Test
    Try
        Dim MyClass As clsTest
        Dim Facilities As Int=2
        MyClass.Initialize
        
        For i =1 To Facilities
            
            MyClass.FacilityID=i
            MyClass.Sub1
            Sleep(2000)
            
        Next
        
    Catch
        Log(LastException)
    End Try
End Sub

Let say I have a list of customer's facilities and I need to send request for each co-job in customer order for each facility.

It works as I expected if I use Sleep(2000) line.

Sub1 Started for Facility 1
CallJob1 Called for CoJob 1
*** Service (httputils2service) Create ***
** Service (httputils2service) Start **
Response 1
CallJob1 Completed for CoJob 1
CallJob1 Called for CoJob 2
Response 2
CallJob1 Completed for CoJob 2
Sub1 Ended for Facility 1
Sub1 Started for Facility 2
CallJob1 Called for CoJob 1
Response 1
CallJob1 Completed for CoJob 1
CallJob1 Called for CoJob 2
Response 2
CallJob1 Completed for CoJob 2
Sub1 Ended for Facility 2


But without this Sleep line the whole workflow is broken - Sub1 never ended for Facility 1

Sub1 Started for Facility 1
CallJob1 Called for CoJob 1
Sub1 Started for Facility 2
CallJob1 Called for CoJob 1
Response 1
CallJob1 Completed for CoJob 1
CallJob1 Called for CoJob 2
Response 1
CallJob1 Completed for CoJob 2
Sub1 Ended for Facility 2
Response 2


So my question is - is there any way to check when Sub1 ended for each facility without using Sleep? I don't know number of seconds that each iteration will take and I don't want to create unnecessary delay but increasing number milliseconds for Sleep.

Thanks.
 

Attachments

  • TimerTest.zip
    10.1 KB · Views: 99
Top