tags:

views:

38

answers:

2

In researching options for getting better than the 15 ms resolution provided by .NET timer objects (see http://stackoverflow.com/questions/3744032/why-are-net-timers-limited-to-15-ms-resolution), I am looking at the different timer objects that Windows provides. I have identified the following:

Are there other timer types available in the Windows API?

A: 

Your best bet for granularity in timer is to build the required function yourself using QueryPerformanceFrequency and QueryPerformanceCounter

Steve Townsend
That would work if I was trying to time a process: that is, determine how long it takes for something to execute. But I'm interested in objects that give me a tick (issue a callback or set an event) on a regular basis. Building one of those with `QueryPerformanceCounter` would be possible, but would require a spinning loop.
Jim Mischel
@Jim - agreed. Multimedia timers purport to offer the best in that line, although the docs indicate that they are now superseded by timer queue timers.
Steve Townsend
@Steve: And they're trying to push Threadpool timers as a replacement for the timer queue timers. It turns out that all the timers (except the first, which I didn't test) will give you a pretty reliable 1 ms interval. I don't know of any other timer types, or any way other than a spinning loop to get better than 1 ms resolution.
Jim Mischel
@Jim - interesting. Honestly, I would post your investigation results as an answer to your own question. I would upvote it, for one.
Steve Townsend
+1  A: 

I asked this question because I was interested in implementing a periodic timer that would give me better granularity than the timers that are supplied with .NET. My investigation of those timers (Windows.Forms.Timer, System.Timers.Timer, and System.Threading.Timer) shows that the best I can hope for is 15 ms granularity, and accuracy of -1 to +30 ms. That's fine for most applications, but not for the application I was working on.

For details of my investigation, see http://stackoverflow.com/questions/3744032/why-are-net-timers-limited-to-15-ms-resolution and http://www.informit.com/guides/content.aspx?g=dotnet&seqNum=815.

That led me to looking for periodic timer objects available under Windows. I identified the five types that I posted in the original question. I didn't find any others. I discarded the old-style Windows timer because I don't want to be processing messages. I then wrote managed prototypes in C# for the other four timer types and did some testing.

All four timer types (Multimedia timers, Waitable timers, Timer queue timers, and Threadpool timers) give a reliable 1 ms interval, with very good resolution. Of those, the Threadpool timer is by far the easiest to interact with, but unfortunately it's not supported on Windows XP. Timer queue timers have a dizzying array of options, but if you ignore most of the options they're almost as simple as Threadpool timers. See Exploring options for better timers for more info.

I chose to wrap the timer queue timer for my general timer class in .NET. You can see it here.

You might also be interested in Waitable Timers in .NET with C#.

Jim Mischel