Android Question timeout for "Wait For"

rdkartono

Member
Licensed User
Longtime User
Using "Wait For", is there somekind of timeout ?
to make a limit that it need to wait for xx seconds maximum... if exceed that time, then something is error.

or if I create a timer with a specified xx seconds,and enabled it before i use "Wait For", after timer_tick, how to disable this "Wait For" ?
 

rdkartono

Member
Licensed User
Longtime User
for example like this dummy code :

Public Sub DoTask1
asyncsend("xxxxx")
wait for asyncreceive
asyncsend("yyyyy")
End Sub

Private Sub async_NewData (Buffer() As Byte)
dim cmd as string = BytesToString(Buffer,0,Buffer.length,"UTF8")
if cmd = "abc" then callsub(Me,"asyncreceive")
End Sub


what happened if async_NewData is never arrive, or arrive with data I dont want. Then "wait for asyncreceive" will wait there, and never continue, right ?
If i need to wait this reply maximumly xx seconds, if not receive then something wrong happened, ... how to do it ?

Of course maybe a timer can be created to trigger timeout, but then how I can destroy that "wait for" ?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Please use [code]code here...[/code] tags when posting code.

what happened if async_NewData is never arrive, or arrive with data I dont want. Then "wait for asyncreceive" will wait there, and never continue, right ?
It will never continue but it doesn't mean that something bad will happen. At some point you will call DoTask1 again and it will replace the previous waiting resumable sub.

You can implement a timeout with this code:
B4X:
Public Sub DoTask1
Dim StopTimeout() As Boolean = Array As Boolean(False)
TaskTimeout(5000, StopTimeout)
AsyncSend("xxxx")
Wait For AsyncReceive(Success As Boolean)
If Success = False Then 'timeout
 'Return
End If
AsyncSend("xxxx")
Wait For AsyncReceive(Success As Boolean)
If Success = False Then 'timeout
 'Return
End If
StopTimeout(0) = True
End Sub


Private TaskTimeout(ms As Int, StopMe() As Boolean)
 Sleep(ms)
 If StopMe(0) = False Then CallSubDelayed2(Me, "AsyncReceive", False)
End Sub

BTW, always prefer CallSubDelayed over CallSub when used together with Wait For.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…