Android Question Chronometer in miliseconds

ivavilagu

Member
Licensed User
I have coded a little chronometer showing milliseconds (the library Clocks doesn´t work with miliseconds). It works but It is a little bit slow and doesn´t count the correct time. This is the code:

B4X:
dim lbTIME as label
dim timer1. as timer
dim countSEC as long
dim countMM as long

timer1.initialize("Timer1", 1)
countSEC = 0
countMM = 0

sub timer1_Tick
    countMM = countMM + 1
    if (countMM = 100) then
          countMM = 0
          countSEC = countSEC + 1
    end if
    lbTIME.Text = NumberFormat(countSEC, 2, 0) & ":" & NumberFormat(countMM, 2, 0)
end if
is there any way to execute the timer_Tick event faster?
 

sorex

Expert
Licensed User
I think it's better to use 10ms for the running timer and show the full ms detail.

it will show different value anyway (not rounded to 10 values I mean) since there are things in the background delaying your timer.
 

ivavilagu

Member
Licensed User
i also do think that is is NOT a good idea to force 1000 Events in one Second in B4A
Yes, I do too, but Android Chronometer shows miliseconds.
 

sorex

Expert
Licensed User
the entire idea is actually wrong.

you should store the start ticks and at each timer even you should do currenttick-starttick to get the right time.
 

ivavilagu

Member
Licensed User
like I said it will show miliseconds too is you update it 10 times a second.
You're agree. I`m changing the code.
 

ivavilagu

Member
Licensed User
the entire idea is actually wrong.

you should store the start ticks and at each timer even you should do currenttick-starttick to get the right time.
The problem is the same, I will do current tick-starttick every milisecond
 

sorex

Expert
Licensed User
what problem is the same?

use it like this

B4X:
dim starttime as long=DateTime.Now
and in your timer

B4X:
Dim t As String
t=DateTime.Now-starttime
t=Floor(t/1000)&":"&Floor((t Mod 1000)/10)
lbTIME.Text=t
 

ivavilagu

Member
Licensed User
Obviously, if I update 10 times every second It will not show milisecons
what problem is the same?

use it like this

B4X:
dim starttime as long=DateTime.Now
and in your timer

B4X:
Dim t As String
t=DateTime.Now-starttime
t=Floor(t/DateTime.TicksPerSecond)&":"&Floor((t Mod 1000)/10)
lbTIME.Text=t
Thank you for the code but the problem is to update the label every centisecond. Your code and mine works fine with deciseconds (0.1 second) but if I try with centiseconds (0.01 second), as you said, It forces to execute the tick event 100 times every second and this is the reason why the chronometer doesn´t work

I can work with centiseconds in order to mesure the correct time.
I'm forced to work with deciseconds in order to show the correct time.
 

sorex

Expert
Licensed User
if you want mili you just use

B4X:
t=Floor(t/DateTime.TicksPerSecond)&":"&Floor(t Mod 1000)
 

sorex

Expert
Licensed User
in your initial code it couldn't work either since you used

B4X:
    if (countMM = 100) then
          countMM = 0
I think the problem is your interpretation of all this.
It's not if you want to go from deci seconds to centi that you need to make the timer run 10 times faster.

just change the extraction from the time difference.
 

ivavilagu

Member
Licensed User
Sorry, maybe I don´t explain well in English (I´m from Spain). I modified your code and works!!!

B4X:
t=Floor(t/DateTime.TicksPerSecond)&":"& numberformat((Floor(t Mod 100)),2,0)
Thanks!!!!
 

DonManfred

Expert
Licensed User
like I said it will show miliseconds too is you update it 10 times a second.
You suggested 10ms so it´s firing 100 events a second.
But you are right... using 100ms (10 events a second) or 200ms (5 events a second) should be enough. Even for a chronometer if it is coded right...
 

udg

Expert
Licensed User
Are you aware of this lib by Jem Miller?

udg
 
Last edited:

ivavilagu

Member
Licensed User
With the help of Sorex I have made a chronometer showing centiseconds digits on screen
 
  • Like
Reactions: udg

Troberg

Well-Known Member
Licensed User
Remember, what you measure is one thing, how often you show it is another. So, you can measure with millisecond accuracy, but only update the screen at decisecond intervals. It's not like anyone will be able to read the digits at 1000 Hz (especially as the screen only updates at maybe 50Hz...).

However, once you press stop (or whatever you do when you want the exact measurement, then you read the time at that precise moment.

For example, it's common to do the opposite when doing minute accuracy clocks. Even though you only show minute accuracy, you update the screen at second accuracy, to prevent some odd effects if a timer event is delayed (Which happens. Remember, a timer is not an exact tick, it's more like "wait this long and then get back to me when you have time for it".), which could mean that one minute was skipped because it didn't get an event because the event got pushed to the next minute.

Another example, which I often use, is in status displays where I display "x objects of y done". These can often get quite flickery, if the operations are quick, which I solve by only update the screen once per second. Sure, the numbers will "skip", but they will be readable and not cause epileptic seizures.

So, think about disconnecting the measurement from the display, and do each as good as you can depending on their different needs.
 
Top