Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What periodic timer objects are available in the Windows API?

Tags:

timer

winapi

In researching options for getting better than the 15 ms resolution provided by .NET timer objects (see https://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:

  • Old style Windows timer
  • Multimedia timers
  • Waitable timer
  • Timer queue timers
  • Threadpool timers

Are there other timer types available in the Windows API?

like image 304
Jim Mischel Avatar asked Sep 21 '10 19:09

Jim Mischel


1 Answers

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 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#.

like image 150
Jim Mischel Avatar answered Sep 27 '22 16:09

Jim Mischel