Alain V
Alain V

Reputation: 331

How to giva a time accurate timer in Fire Monkey?

I have to display a timer in 10th second for a sport competition. I have do this using the OnTimer event of a TTimer. the interval is set to 100. My routine display the current min:sec.10th (ex.: 02:45.7 ) correctly but it seem that my timer loose about 4 second at each minutes if I comp. to normal clock.

There is a better way to get a time accuracy timer in Delphi XE2 (or XE3) ?

Upvotes: 1

Views: 2244

Answers (1)

jachguate
jachguate

Reputation: 17203

You can use a timer to display the current value of the clock, but use a different approach to calculate the elapsed time.

You have to know that Windows timers are not time accurate, and even if you set it to elapse every 100 milliseconds, it can take more to fire the OnTimer event and even it can miss some intervals if for some reason elapses two or more times before your application process it.

You can, for example, use the system high-resolution performance counter to track times with nano-second accuracy.

You can also use the Delphi TStopwatch class, which encapsulates the system calls and falls back to other method (GetTickCount) if the high resolution performance counter is not available in your machine.

Take also a look at the How to Accurately Measure Elapsed Time Using High-Resolution Performance Counter delphi.about.com article.

Upvotes: 2

Related Questions