The stopwatch is the highest resolution timer available and goes down to 100nano-second resolution.

The time cost of the Gpio.Write and Gpio.Read methods can be an issue with time sensitive things.

Using Stopwatch To Accurately Delay


using System.Diagnostics;

	private Stopwatch Stopwatch1;

	Stopwatch1 = Stopwatch.StartNew();

	StopwatchWait(5);		//Delay in mS


	//*************************************
	//*************************************
	//*********** STOPWATCH WAIT **********
	//*************************************
	//*************************************
	//A synchronous wait is used to avoid yielding the thread.  This method allows for very precise timing.
	private void StopwatchWait(double milliseconds)
	{
		//Calculates the number of CPU ticks that will elapse in the specified time
		long initialTick = Stopwatch1.ElapsedTicks;
		long initialElapsed = Stopwatch1.ElapsedMilliseconds;
		double desiredTicks = milliseconds / 1000.0 * Stopwatch.Frequency;
		double finalTick = initialTick + desiredTicks;

		//Wait in a loop until the threshold is hit
		while (Stopwatch1.ElapsedTicks < finalTick)
		{

		}
	}

	private void Page_Unloaded(object sender, RoutedEventArgs e)
	{
		Stopwatch1.Stop();

Creating a high priority thread that runs forever with a high accuracy stopwatch timebase

Note this isn't perfect!!  We've found there will be pauses of lots time (> 200mS) every now and then,presuambly when the OS is up to something else.  


using System.Diagnostics;

	Stopwatch Stopwatch1;

	//----- START OUR HIGH PRIORITY BACKGROUND THREAD -----
	//We don't await it as its going to run for the lifetime of the application in the background
	Stopwatch1 = Stopwatch.StartNew();
	Windows.System.Threading.ThreadPool.RunAsync(this.MyHighPriorityBackgroundThread, Windows.System.Threading.WorkItemPriority.High);


	//******************************************************
	//******************************************************
	//********** HIGH PRIORITY BACKGROUND THREAD ***********
	//******************************************************
	//******************************************************
	private void MyHighPriorityBackgroundThread(Windows.Foundation.IAsyncAction action)
	{
		//This thread runs on a high priority task and loops forever
		while (true)
		{
			//DO SOMETHING...


			//Delay using high accuracy timer
			StopwatchWait(5);
				
		}
	}


	//*************************************
	//*************************************
	//*********** STOPWATCH WAIT **********
	//*************************************
	//*************************************
	//A synchronous wait is used to avoid yielding the thread.  This method allows for very precise timing.
	private void StopwatchWait(double milliseconds)
	{
		//Calculates the number of CPU ticks that will elapse in the specified time
		long initialTick = Stopwatch1.ElapsedTicks;
		long initialElapsed = Stopwatch1.ElapsedMilliseconds;
		double desiredTicks = milliseconds / 1000.0 * Stopwatch.Frequency;
		double finalTick = initialTick + desiredTicks;

		//Wait in a loop until the threshold is hit
		while (Stopwatch1.ElapsedTicks < finalTick)
		{

		}
	}

 

UK Electronic Designer
USEFUL?
We benefit hugely from resources on the web so we decided we should try and give back some of our knowledge and resources to the community by opening up many of our company’s internal notes and libraries through mini sites like this. We hope you find the site helpful.
Please feel free to comment if you can add help to this page or point out issues and solutions you have found, but please note that we do not provide support on this site. If you need help with a problem please use one of the many online forums.