Re: Multimedia Timer
"clinisbut" <clinisbut@gmail.com> wrote in message
news:ac5877e2-3c6d-4c4e-9d05-8b9a58de16c6@e4g2000hsg.googlegroups.com...
I need to execute some code every 20ms. This code is just send some
data through serial port, so I think it's not heavy.
At first I was using SetTimer function and OnTimer event, but with no
good results: seems that not sending every 20ms, instead is sending 3
or so consecutive frames at same time.
Today I tried the multimedia timers, and seems that does the same.
Inside my TimeEventHandler, I trace:
TRACE( "Time:%d\n", GetTickCount() );
And I see that sometimes event handler fires up more than one time at
same time, look:
Time:30664983
Time:30664983
Time:30664983
Time:30664993
Time:30665013
Time:30665033
Time:30665054
Time:30665074
I'm measuring correctly the time elapsed between calls?
The 'resolution' claimed by the multimedia timer is not the same as the
resolution provided by GetTickCount. GetTickCount is only updated at the
system clock rate, which is much longer than a millisecond. So GetTickCount
will not give you good measurements for fast events.
20 milliseconds is also rather fast for attempting to do anything like
output to the serial port. Your thread may not be run, even though 20
milliseconds have elapsed, because other higher-priority threads may need to
be run.
For better measurements you can use QueryPerformanceCounter. For better
periodic timing you would probably have to adjust priorities with
SetThreadPriority.
--
Scott McPhillips [VC++ MVP]