Stopatch is an accurate timeout timer, but it doesn’t Generate Events

The stopwatch timer accurately measures elapsed time.
If the PC supports a high-resolution performance counter, then the Stopwatch class uses that counter to measure elapsed time. Otherwise it uses the system timer. Use the Frequency and IsHighResolution fields to determine the precision and resolution if desired.

using System.Diagnostics;

	private Stopwatch Stopwatch1;

	//----- START OUR HIGH ACCURACY TIMER -----
	Stopwatch1 = new Stopwatch();
	Stopwatch1.Reset();
	Stopwatch1.Start();
	if (Stopwatch1.ElapsedMilliseconds > 500)       //<< Set mS timeout value
	{

	}

	//If you find the timer stops on its own for some reason do this:
	if (!Stopwatch1.IsRunning)
	{
		Stopwatch1.Reset();
		Stopwatch1.Start();
	}

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;
using System.Threading;

	private const int DelayPerUpdateForMs = 20;

	private long StopwatchElapsedMillisecondsLast = 0;
	private int OurDelayForThisUpdateMs;


	//----- START OUR HIGH PRIORITY BACKGROUND THREAD -----
	//This thread is going to run for the lifetime of the application in the background
	Stopwatch1 = Stopwatch.StartNew();
	Thread newThread = new Thread(MyHighPriorityBackgroundThread);
	newThread.Priority = ThreadPriority.Highest;
	newThread.Start();



	//**************************************************************
	//**************************************************************
	//********** HIGH PRIORITY BACKGROUND LOOPING THREAD ***********
	//**************************************************************
	//**************************************************************
	private void MyHighPriorityBackgroundThread()
	{
		//This thread runs on a high priority task and loops forever
		while (true)
		{
			try
			{
				//------------------------------------------
				//----- SETUP DELAY BEFORE NEXT UPDATE -----
				//------------------------------------------
				Thread.Sleep(DelayPerUpdateForMs);        //Delay in mS.

				OurDelayForThisUpdateMs = Convert.ToInt32(Stopwatch1.ElapsedMilliseconds - StopwatchElapsedMillisecondsLast);
				StopwatchElapsedMillisecondsLast = Stopwatch1.ElapsedMilliseconds;
				if (OurDelayForThisUpdateMs < DelayPerUpdateForMs)
					OurDelayForThisUpdateMs = DelayPerUpdateForMs;

				//DelayPerUpdateForMs			//<<<How long we delay for per update
				//OurDelayForThisUpdateMs		//<<<How long we actaully delayed for this time (there can be variation to to OS delays doing other things)

				//----- WE ARE HERE EVERY 20mS -----




			}
			catch (Exception )
			{

			}
		} //while (true)
	}

Feel free to comment if you can add help to this page or point out issues and solutions you have found. I do not provide support on this site, if you need help with a problem head over to stack overflow.

Comments

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