Demystifying Timer Class

Often in desktop applications we need to keep a particular process running at regular interval. And the only options which we are left with for it, are the timers. So basically the timers which we are provided with are divided into three and they are

  • System.Windows.Forms.Timer
  • System.Timers.Timer
  • System.Threading.Timer

So each of timer has got its own uses. But in this section we will look into the limitation of the System.Windows.Forms.Timer and its alternative.

BRIEF

System.Windows.Forms.Timer:

As the word Timer  itself describes, it deals with an event that gets repeated at regular intervals. System.Windows.Forms.Timer is designed to work under a single threaded environment to meet the functionalities of timer. In case of this timers, timer interval are set which elapses between ticks depending upon the interval value set, calling the event mentioned to its Tick event.Two methods related to System.Windows.Forms.Timer are  Start and Stop which determine the starting and stopping of the  timer.

System.Timers.Timer:

As it is one of the timer, its mechanism will be the same as above but as compared to Tick event in System.Windows.Forms.Timer, System.Timers.Timer works with elapsed event. It does have start and stop method for starting and stopping the  timer. It overcomes the limitation of System.Windows.Forms.Timer  in terms of accuracy and multi threading environment aspect.

System.Threading.Timer:

It is the simplest timer out of the three. While construction of the same we need to mention the due time and interval. The timer will get fired first  after waiting for the due time mentioned ,there after the timer will fire depending upon the interval mentioned.There is no start and stop method the functionality of both the method can be adjusted using due time.

Issues System.Windows.Forms.Timer :
As we are working with timer, we definitely need to look into intervals depending upon which the process will be executed again. So the issues come up with specifying upon the timer interval in System.Windows.Forms.Timer so moving ahead let me bring one important aspect of this and that is

System.Windows.Forms.Timer  is designed to work in single threaded environment and its  accuracy limitation is  55 millisecond.

So from the statement only we can get to know that this timer is a single threaded and in case where multi threading we can’t use the same. But second and most important consideration is the measure of accuracy.

The scenario is similar when we are setting the timer interval in case of System.Windows.Forms.Timer

Let’s look at the same through an example

// Declaring a Timer

System.Windows.Forms.Timer testTimer = new System.Windows.Forms.Timer();

// Assigning Event That Will Get Executed In Each Tick Of The Timer

testTimer.Tick += new EventHandler( TestTimerEventProcessor );

// Setting Interval For the Timer or Time Span between Next Tick

testTimer.Interval = 750;

// Starting a Timer

testTimer.Start();

  ANALYZE  

 Till declaration, assigning event on tick , starting timer it is fine but the setting intervals has lot of limitations and drawbacks. Here we have set the interval to be 750 but from the points mentioned above System.Windows.Forms.Timer limited to an accuracy of 55 milliseconds . So if the accuracy is 55 milliseconds than timer being set to an interval of 750 is not going to give expected result. The reasons behind in inaccurate execution of this timer is:  

  1. Timer event is dependent upon any event or system event , long loops, drive network or port access. So the execution of event wont be on particular interval as specified during interval property specification.
  2. Other drawback is the range with which we can set our timer interval and that is between 1 – 64,767 milliseconds so if we cant set the interval longer than one minute .(1 second= 1000 milliseconds, 64,767 milliseconds = 64.767 second = 64.8 second(approx)).
  3. Again more than the elapsing of timer interval is not guaranteed which will again lead to unexpected result.
  4. As we know that system generates 18 clock ticks per second. Measuring that even if the interval will be measured in millisecond we won’t get true precision of an interval exceeding  one-eighteenth of a second.

So lets take a situation where there are two buttons- btnStart and btnStop. Initially we have declared timer

static System.Windows.Forms.Timer testTimer = new System.Windows.Forms.Timer();

While Initializing Component we are determining the behavior of two buttons as mentioned below

private void InitializeComponent() 
 {

// Enabling Components

btnStart.Enabled = true;
btnStop.Enabled = false;
}

Again on the click event of the btnStart we have started the timers working

protected void btnStart_Click(object sender, EventArgs e)
{

// Assigning Event That Will Get Executed In Each Tick Of The Timer

testTimer.Tick += new EventHandler( TestTimerEventProcessor );

// Setting Interval For the Timer or Time Span between Next Tick

testTimer.Interval = 750;

// Starting a Timer

testTimer.Start();

}

Further more lets assign enabling of btnStop in in the process TestTimerEventProcessor such as

private void TestTimerEventProcessor ( Object myObject, EventArgs myEventArgs )
 

{
// Stopping the Timer
testTimer.Stop();
// Enabling the btnStop Button
btnStop.Enabled = true;
// Starting the Timer again
testTimer.Start();
}

So from the above scenario my btnStart and its click event is going to start the timer assigned and the tick interval of the same is called depending upon the interval mentioned that is 750 milliseconds but looking into inaccurate measurement or interval of System.Windows.Forms.Timer and its dependency on other CPU processing my second button that is btnStop enabling will be unpredictable with the time measure.

Further more if any keyboard message is dependent to the enabling of btnStop than that event wont work accurately for example

protected override bool ProcessKeyPreview(ref System.Windows.Forms.Message m) 

{ 

// Code for Tracking Keyboard event

// Switch Case for Different key Press

//Below is one of switch case

 

if(this.btnStop.Enabled)

{

// Do Something

}

}

Than the prediction of keyboard event will be very different and output will come but depending upon CPU usage.

SOLUTION

The probable solutions for this particular issue are –

a. We can keep the interval below 55 milliseconds so that our interval will fall within the range of accuracy.

b. Second option can be the use System.Timers.Timer. which is more accurate than that of System.Windows.Forms.Timer

150 150 Burnignorance | Where Minds Meet And Sparks Fly!