I'm making an app that needs some pretty tight timing and the Stopwatch class is the perfect solution. However, I noticed sometimes, when running on a small panel PC, the Stopwatch values veer way off. I added some debug printouts that monitor the value of the stopwatch every 200 ms or so:
0:00:197
0:00:502
0:00:702
...
0:03:356
0:12:93
0:13:21
0:13:421
...
How could it possibly jump from ~3 seconds to ~13 seconds? I now see that the underlying function QueryPerformanceCounter() is buggy (http://www.virtualdub.org/blog/pivot/entry.php?id=106), but I get the sense that something else is going on here.
Any insight is appreciated.
Update:
Here's a little more detail on my code: it's pretty straightforward. It's a WPF app that creates a new Stopwatch
object on startup, then kicks it off via Start()
. I then create a DispatcherTimer
like so:
displayTimer = new DispatcherTimer();
displayTimer.Tick += display_Tick;
displayTimer.Interval = DISPLAY_INTERVAL_TIMESPAN;
where the timespan is 200 ms. My debug code simply prints out the value of the Stopwatch
object every time the dispatchTimer
ticks.
Here is a link to a debug log: dl.dropbox.com/u/7999907/hburg.txt. The first bit of data on each line is the current date/time via DateTime.UtcNow
. The time all the way to the right is the value via Stopwatch.Elapsed
. You can see how the stopwatch jumps but the DateTime doesn't.
Update2:
Here's a fun Microsoft support article: http://support.microsoft.com/kb/274323