This code is meant to be a countdown timer that should trigger a beep every full second.
I've tried a "timer" and a "do while" loop, but the performance is not so good in both.
The beep has a delay of up to 2 hundredths of a second.
Is there a way to reduce this delay?
Here is my new example with the Threading-Library, it looks much better.
Code:
B4X:
Sub Run_BeepThread
Dim arg(0) As Object
Dim obj(1) As Object
Do While IsBeepEnabled
IntervalMSec = lgTime - (DateTime.Now - StartTickCount)
obj(0) = IntervalMSec
beepThread.RunOnGuiThread("Update_ClockLabel",obj)
If IntervalMSec <= restzeit Then
If restzeit > 0 Then
Log("Thread-IntervalMSec= " & IntervalMSec)
beepThread.RunOnGuiThread("Start_beep200", arg)
Else If restzeit = 0 Then
beepThread.RunOnGuiThread("Start_beep500", arg)
beepThread.RunOnGuiThread("btnStop_Click",arg)
End If
Dim rest As Long = (restzeit - IntervalMSec) / 1000
'Log("rest=" & rest)
If rest > 1 Then
restzeit = restzeit - (rest * 1000 + 1000)
Else
restzeit = restzeit - 1000
End If
End If
beepThread.Sleep(5)
Loop
End Sub
I dont know what exactly u r doing but using a timer with 5ms is not the right way.
If it is very very very important for u to get every x time the beep u could use a game engine that uses the phone hardware. it will give u a better result and u can use it also for your label animation instead of a timer. Try acclerated surface.
A timer with 5ms will not really update every 5ms at least not on all phones or even on most phones.
I am now at work but when i get to my pc i will look at your example and see if i can get a better result. Does your example also include the label update function?
Do u really need 5ms for the update? U know that 16ms means 60 frames/sec what means that all games runs on 60 frames per sec and thats smooth enough for the human eye, unless u r targeting also flies
Thank you for your example.
I have tested it and as you can see from my screenshot, I have a very different result.
Do u really need 5ms for the update? U know that 16ms means 60 frames/sec what means that all games runs on 60 frames per sec and thats smooth enough for the human eye, unless u r targeting also flies
as i said before, i would use for that a simple timer with interval 1000 or the sleep(1000) function and changed the label to show "HH:mm:ss" and like that update it every 1 seconds instead of showing also ms.
anyway the AC example is also good enough for your needs. you can see that from start to end it took 10026 ms in your phone so 26ms delay for 10 sec is 2.6ms for each sec in average so it is inside the +/- 5ms
but i would still use a simple timer for that with 1000 ms interval if you just want to have a beep after 1 sec.
you can also try nanotime library where you can get a much precise time (nano seconds!!!)
anyway the AC example is also good enough for your needs. you can see that from start to end it took 10026 ms in your phone so 26ms delay for 10 sec is 2.6ms for each sec in average so it is inside the +/- 5ms
it will depend on what phone your app is running, i doubt that it will run the same on all phones.
but i also doubt that there is a 100% countdown app some where so 5ms is really not that bad filippo.
How Fast Is a Blink of an Eye? The average duration for a single blink of a human eye is 0.1 to 0.4 seconds, or 100 to 400 milliseconds, according to the Harvard Database of Useful Biological Numbers.Apr 24, 2017
The people who use my app usually use such instruments, and expect my app to do about the same.
Each beep must come exactly on the hundredth of a second.
These old-time races are about the accuracy and not the speed.
There are penalty points for every hundredth of a second.
Well, with this trick I have now improved my code.
All you have to do is update the label "lblClock" every 100ms and the world looks completely different.
The timer now gives just as good results as the threading library.