B4J Question How to set a timeout for a time consuming sub

xulihang

Well-Known Member
Licensed User
Longtime User
I have a sub that normally takes less than 10 seconds but may take more than 5 minutes in some cases. How to set a timeout for such a sub?
 

xulihang

Well-Known Member
Licensed User
Longtime User
I am using it with the Threading library.

B4X:
Sub CallingSub
    DoProcessingAsync(boxesList,process)
    wait for Localized(Success As Boolean)
End Sub

Sub DoProcessingAsync(boxesList As List,process As List) As ResumableSub
    Dim b() As Boolean = Array As Boolean(False)
    TimeOutImpl(1000, b)
    th.Start(Me,"DoProcessing",Array As List(boxesList,process))
    wait for th_Ended(endedOK As Boolean, error As String)
    If b(0) = False Then CallSubDelayed2(Me, "Localized", True)
    Log(endedOK)
    Log(error)
    Return endedOK
End Sub

Sub TimeOutImpl(Duration As Int, b() As Boolean)
    Sleep(Duration)
    If b(0) = False Then
        Log("time out")
        CallSubDelayed2(Me, "Localized", False)
    End If
End Sub
 
Upvote 0
Top