The normal timers (system::timers::timer) are not accurate on fast timings. The thread sleep instruction timer also is often not. A minimum resolution may be 15mS or it may 55mS. Also it seems that if you specify a time of say 10mS, this will get rounded up to match the nearest time resolution – basically crap! A good resource on this: http://www.codeproject.com/KB/cs/LP_TimerTest.aspx

Pausing In A Function

See sleep in threads

Global Timers

time_since_start = (clock_timer – start_at_time_value);

Accurate timeout timer – Doesn’t Generate Events

The stopwatch timer accurately measure 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.

Use the System.Diagnostics.Stopwatch class
	using namespace System::Diagnostics;

	Stopwatch ^MyTimeoutTimer;
	MyTimeoutTimer = gcnew Stopwatch;

	MyTimeoutTimer->Reset();
	MyTimeoutTimer->Start();

	if (MyTimeoutTimer->ElapsedMilliseconds > 500)		//<< Set mS timeout value
	{

	//If you find the timer stops on its own for some reason do this:
	//(it was needed for Kinesys Libra)
	if (!MyTimeoutTimer->IsRunning)
	{
		MyTimeoutTimer->Reset();
		MyTimeoutTimer->Start();
	}

General Timer – Creating when not on a form

Setup
	System::Windows::Forms::Timer ^MyTimer;
	MyTimer = gcnew System::Windows::Forms::Timer();
	MyTimer->Interval = 250;
	MyTimer->Enabled = true;
	MyTimer->Tick += gcnew EventHandler(this, &MyClassName::MyTimerFunction);
Function prototype
	void MyClassName::MyTimerFunction(System::Object^  sender, System::EventArgs^  e);
Function
	void MyClassName::MyTimerFunction(System::Object^  sender, System::EventArgs^  e)
	{
	}

Not so accurate timeout timer

Note that this timer has a worst case resolution of 500mS so not good for fast timing requirements

	int StartTime;
	StartTime = System::Environment::TickCount;
	while (1)
	{
		System::Windows::Forms::Application::DoEvents();	//(Neded in case what your waiting for occurs on same thread)
		if ((System::Environment::TickCount - StartTime) > 500)		//<< Set mS timeout value
		{
			//TIMED OUT
		}
	}

Multimedia timers

Allow applications to schedule timer events with the greatest resolution (or accuracy) possible for the hardware platform. These timer services are useful for applications that demand high-resolution timing. For example, a MIDI sequencer requires a high-resolution timer because it must maintain the pace of MIDI events within a resolution of 1 millisecond.
Each multimedia Timer instance runs in its own thread; it doesn’t matter which thread the Timer was created in.
Warning – this type of timer has the potential to cause problems as its so powerful. If your calling it very fast then ensure the function that is called every roll over is fast.
The Win32 multimedia timer is not part of the .NET Framework. However, by using the .NET interoperability services, the multimedia timer can be brought into the .NET fold.
Note that MSDN says this type of timer is obsolete and to use another timer. However without this timer there is no other timer that allows you to create evetns with this level of accuracy. The only other accurate timer is the stopwatch class but that can’t be used to create events. Therefore it makes sense to use this even with the obsolte notive as its not obsolete yet and they will need to provide something instead if they do obsolete it.

Int Note – We have a class that provides this timer for the Int Circ Array Tracker Range Finder project. Lots of time spent researching. These we’re the 2 very helpful links code is based on:
http://www.codeguru.com/forum/showthread.php?t=385546
http://www.codeproject.com/KB/cs/LP_TimerTest.aspx
http://www.codeproject.com/KB/miscctrl/lescsmultimediatimer.aspx

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 *