Home > .net > Increasing the timer resolution

Increasing the timer resolution

Raymond Chen’s blog post on Timers reminded me about the timeBeginPeriod and timeEndPeriod APIs. It turns out Larry Osterman also blogged about them here.

While coding some applications, for example, a market data generator, you might want to increase the timer resolution so that you can genereate higher volume of data.  This is where these APIs come into play.

[DllImport("winmm.dll")]
internal static extern uint timeBeginPeriod(uint period);

[DllImport("winmm.dll")]
internal static extern uint timeEndPeriod(uint period);

int resolution = _randomSleep.Next(0, 5);
try {
   // change minimum timer resolution
   timeBeginPeriod((uint) resolution);
   Thread.Sleep(resolution);
}
finally {
   // reset the timer
   timeEndPeriod((uint) resolution);
}

There are 2 points to keep in mind (as mentioned in Larry’s blog post and in the documentation):

  1. This is a global setting. So it could affect other applications.
  2. It can reduce system performance.
Advertisements
Categories: .net
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: