Can SetTimer() (mfc) be used in real-time applications?  
Author Message
EnigMa_AnGeL





PostPosted: Visual C++ General, Can SetTimer() (mfc) be used in real-time applications? Top

I'm developing a time-series data acquisition system that interfaces with DAQ hardware which reads and generates voltages through it's channels simultaneously. That means that Input (data acquisition) and output (generation) must be done with accurate timing. Unfortunately, the Generation process must be done using software timing (windows timing). So, i declared a
SetTimer(TimerID, 1000, TimerProc) so that the callback kicks in at each second.

At first, I encountered timing issues that the hardware timed data acquisition was lagging behind generation. I tried redoing that class but then again i tested it to be accurate (i actually used a stopwatch). It was only then that i found out the windows timer had elapsed 3612 seconds after exactly 1 hour, meaning it went ahead 12 seconds by 60 minutes. This caused a lot of problems as I tried adjusting the Elapse between 1001 and 1004 with alternating differences in timing.

I'm still a novice programmer so any advice or prior discussions would be welcome. Thank you.


Visual C++2  
 
 
einaros





PostPosted: Visual C++ General, Can SetTimer() (mfc) be used in real-time applications? Top

I'd look into high resolution timers, and forget about SetTimer(). See http://msdn.microsoft.com/msdnmag/issues/04/03/HighResolutionTimer/.

 
 
Simple Samples





PostPosted: Visual C++ General, Can SetTimer() (mfc) be used in real-time applications? Top

You say that the Windows timer is off by 12 seconds per hour. I think that it is the hardware clock, not the Windows timer that is the problem. Perhaps the software must require an accurate system clock. I think there is a way to adjust the use by Windows of the system clock but I forget the details.

The SetTimer function and related message and functions are not intended to be precise. Certainly you need to at least use more precise timers. Note that, except for special versions of Windows, Windows is not designed for real-time processing. If you must use the non-embedded versions of Windows, then in order to get the most reliability for time-critical tasks, I am nearly certain that you must use a device driver. Don't expect an application program to be able to respond reliably to timer events.



 
 
EnigMa_AnGeL





PostPosted: Visual C++ General, Can SetTimer() (mfc) be used in real-time applications? Top

I'll just cap this thread with what findings i had. Yes, its true that Windows Timer is not accurate. Actually, its also true that its not even meant to be. I finally made a suitable alternative which is by using the Multimedia Timer (functions as timeSetEvent and etc). This worked out pretty well. I also found out (after much prodding) that the problem was a 2-pronged one: That Win Timer itself is not accurate, and that on my Particular pc, the time was off by that many seconds per hour. This was remedied by replacing the timer function calls and the callback block to use MM timer and changing to another testbed altogether. Would make sense to probe into what caused this problem, though I confirmed it to be of hardware nature though.