Android Question Sub Sleep freezes my app

uskomp

Member
Licensed User
Longtime User
Hi,
I tried with smartphones Samsung S5 with Android 6 and Huawei Honor 10 with Android 9

I had license B4A 8.8 but I back to version B4A 6.80 due problem with Sleep.
I bought B4A 9.3 yesterday but problem with Sleep is still (as before - freeze my app, printing OK)

I have to use Sleep due printing via Bluetooth file print.txt (size is 6-8 kB; in mybufx).
It prints without Sleep only first cca 1kB of file (disconnecting is faster than printing all buffer; not freeze my app).

B4X:
In Process_Globals:  Dim mybufx() As Byte
...
mybufx= File.ReadString(dird, "print.txt").GetBytes("windows-1250")
...

Sub Serial1_Connected (Success As Boolean)
   If Success Then                                         
      Dim os1 As OutputStream
      os1=Serial1.OutputStream
      os1.WriteBytes(mybufx, 0, mybufx.Length)
      os1.Flush
      Sleep(2 * mybufx.Length + 4000)   'freeze; too Sleep(2000)
      Serial1.Disconnect
   Else
      MsgBox("Error BT...","")
      Serial1.Disconnect
   End If
End Sub

Thanks for any solution with sub Sleep or with "Wait for".
Please link to B4A version 6.80. I back to old version B4A.
 
Last edited:

uskomp

Member
Licensed User
Longtime User
Problem has been solved with MySleep

Sub MySleep(ms As Long)
ms= DateTime.Now + ms
Do While ms>DateTime.now
DoEvents​
Loop​
End Sub
 
Upvote 0

Erel

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

I'm sorry to say but all of the code above is incorrect.

- Writing directly to a network / Bluetooth stream is a mistake.
- Holding the main thread in a loop is also a mistake.

Both will lead to stability issues.

The correct solution is to use AsyncStreams.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…