Android Question Timer as timeout function

Electrocutioner

Member
Licensed User
Hello All,

I am setting up a command link with an acknowledge.

this code hangs instead of displaying the timeout message.


B4X:
Sub AStream_NewData (buffer() As Byte)

checkbyte1 = buffer(0)
checkbyte2 = buffer(1)
checkbyte3 = buffer(2)
  
End Sub


B4X:
Sub timer1_tick
timeout = 1

End Sub


B4X:
Sub send_CMD_String (address As Int, CMD As Int, DAT As Int)

   Dim sb As StringBuilder
   Dim cmd_String As String
   Dim buffer() As Byte
   
   sb.Initialize
   sb.append(Chr(address)). append(Chr(CMD)) .append(Chr(DAT))
   cmd_String = sb.ToString

   buffer=cmd_String.GetBytes("ISO-8859-1")
   
  
   timeout = 0
   timer1.Initialize("Timer1", 500)
   timer1.Enabled = True
 

   Do While timeout=0
   
     AStream.Write(buffer)
     If (checkbyte1 =  address) And (checkbyte2 = CMD) And (checkbyte3=DAT) Then Exit
   Loop

   If (checkbyte1 <>  address) Or (checkbyte2 <> CMD) Or (checkbyte3 <> DAT) Then
   ToastMessageShow ("COMMUNICATION ERROR", True)
   End If

End Sub
 

qsrtech

Active Member
Licensed User
If i understand android os correctly, your "send" sub with loop holds the main thread and prevents basically anything else from working, i.e. timer. I suspect you're going to have to re-work it and probably use the "newdata" event to check your bytes and cancel any processing in the timer event. It can make things more challenging and messy to maintain not being able to do a loop and wait.
 

RandomCoder

Well-Known Member
Licensed User
This is correct, you should not hold the Main Thread in a Loop for too long as it will ultimately result in an ANR (Application Not Responding) error. It's not all bad though, just a slightly different way of looking at things. For instance you can still send the data and start your time-out timer. If the timer tick event occurs you can abort or resend or whatever it is you had planned for that scenario, and if the newdata event occurs you can stop the timer so that it's tick even never happens and then check your data and continue with the next stage, whether it be sending the next lot of information or manipulating the data received.
 

Electrocutioner

Member
Licensed User
This is correct, you should not hold the Main Thread in a Loop for too long as it will ultimately result in an ANR (Application Not Responding) error. It's not all bad though, just a slightly different way of looking at things. For instance you can still send the data and start your time-out timer. If the timer tick event occurs you can abort or resend or whatever it is you had planned for that scenario, and if the newdata event occurs you can stop the timer so that it's tick even never happens and then check your data and continue with the next stage, whether it be sending the next lot of information or manipulating the data received.

Thanks for the replies -- the small interruption is not of any consequence during the button press in my application.

here's my latest Code

B4X:
Sub AStream_NewData (buffer() As Byte)

checkbyte1 = buffer(0)
checkbyte2 = buffer(1)
checkbyte3 = buffer(2)
  
End Sub



'______________________________________________________________________________________________
Sub send_CMD_String (address As Int, CMD As Int, DAT As Int)

   Dim sb As StringBuilder
   Dim cmd_String As String
   Dim buffer() As Byte

   Dim count As Int
   sb.Initialize
   sb.append(Chr(address)). append(Chr(CMD)) .append(Chr(DAT))
   cmd_String = sb.ToString

   buffer=cmd_String.GetBytes("ISO-8859-1")
 
   checkbyte1 = 0
   checkbyte2 = 0
 
   For count = 1 To 10
 
      AStream.Write(buffer)
    
      Wait(20)
      If (checkbyte1 =  address) And (checkbyte2 = CMD) Then
    
      DataByte = checkbyte3
      Exit
      End If
     
   Next
 
   If (checkbyte1 <>  address) Or (checkbyte2 <> CMD) Then
   
      DataByte = 0
      ToastMessageShow ("COMMUNICATION ERROR", True)
   End If
 
End Sub


Sub Wait(milliseconds As Int)

   Dim T As Long

   T = DateTime.now +milliseconds

   Do While DateTime.now < T

   DoEvents

   Loop

End Sub
 
Top