Skip to content

How to create a high resolution and accurate timer in C#?

How to create a high resolution and accurate timer in C#?

I had to write some C# code that communicates to an embedded device through the serial port. The specifications of the device firmware were that some commands (such as Deleting devices from groups) had to be sent twice with a gap of less than 40 millisecond. The Thread.Sleep(40) command was used to create the gap but when testing with some old computers we realized that the operation was failing intermittently because the time elapsed is more than 40 milliseconds. Using the StopWatch class I was able to measure the gap and confirmed that when it didn’t work the Thread.Sleep(40) was not 40 milliseconds but 51 milliseconds as shown in the image below.

When it comes to these sorts of commands millisecond error margins will matter. A number of factors will have an effect on the accuracy of Thread.Sleep such as CPU load, process priorities, number of concurrent threads, and even from other processes. So for creating precise time elapses it’s best to avoid using Thread.Sleep and use a more accurate time elapsed gap.

Stopwatch class provides a set of methods to accurately measure elapsed time.

How to determine the execution time of a section of code using the Stopwatch?

Here’s the C# code to determine the execution time of a section of code using the Stopwatch class.

using System;
using System.Diagnostics;
using System.Threading;
class Program
{
     static void Main(string[] args)
     {
         Stopwatch stopWatch = new Stopwatch();
         stopWatch.Start();
         Thread.Sleep(40); // replace this line with your code
         stopWatch.Stop();
         // Get the elapsed time as a TimeSpan value.
         TimeSpan ts = stopWatch.Elapsed;
         // Format and display the TimeSpan value.
         string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:000}",ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds);
         Console.WriteLine("RunTime " + elapsedTime);
      }
}

How to create an accurate time elapsed gap using C# Stopwatch?

Below is the code to create an accurate time elapsed gap using the C# Stopwatch class.

using System;
using System.Diagnostics;
using System.Threading;
class Program
{
     static void Main(string[] args)
     {
         Stopwatch stopWatch = new Stopwatch();
         stopWatch.Start();
         // this line will create accurate 40 milisecond
         while (stopWatch.ElapsedMilliseconds < 40) {}
         stopWatch.Stop();
         // Get the elapsed time as a TimeSpan value.
         TimeSpan ts = stopWatch.Elapsed;
         // Format and display the TimeSpan value.
         string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:000}",ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds);
         Console.WriteLine("RunTime " + elapsedTime);
      }
}

Leave a Reply

Your email address will not be published. Required fields are marked *


The reCAPTCHA verification period has expired. Please reload the page.