Refresh or timer problem


I'm facing a problem in my Android app, where I have to show a simple counter. I do that with the Timer class in my editor, and set it to display the current beat every say 20 ms. The problem is that it's not absolutely constant. If I play around with other controls in the screen, I can see the counter slow, then speed up. What am I doing wrong?

I have tried using the HiRes Timer also (with a MessageManagerLock), but the result is the same.

Thanks for any hint that can help me achieve a fully reliable counter on screen.


No timer that happens on the GUI thread can ever be consistently accurate, because other events may block the event thread for unknown lengths of time. The best you can hope for is to run a timer and measure the elapsed time since the previous one, so that you can progress your animations by the appropriate time rather than a fixed interval.

Actually the timer triggers the refresh frequency, but the exact value displayed is calculated out of a playhead time, with Time::getMillisecondCounter(). So the displayed value should always be accurate (hence the acceleration of the beat count when refreshes occur after a slowdown). 

Anything that can be done to force the refresh of that component (displaying the counter) on a more consistent basis?


I managed to improve the accuracy by fetching the beat count right inside the paint() method and drawing it inside the component's black background. The editor's Timer only calls for repaint() of that component.

I know that it's not advised to add such computing in the drawing method, but it seems to work better than setting the counter value at time x in a text field that will be drawn at time y, when the UI thread is available for that.

Finally that didn't really solve the issue. The problem seems to be in the time required to pass the counter window as pixels to the Java methods in the paint process. Whatever the timer I put, I can't refresh more than 2-3 times per second, which creates a visible glitch.

I thought to use Open GL but felt it was a bit heavy for simply displaying a number. So I ended up adapting my layout to have the counter appear on a Java dialog and poll for the counter value through JNI. I am therefore retrieving a real integer instead of a pixellized representation of it, which obviously goes much faster and makes sense;-)

Have you tried building with 'ant release' rather than debug? 

Made a massive difference for me. I had no idea debug mode slows things down so much...