Timers


Timers are a familiar entity to Visual Basic programmers, as they have been part of that language for many years . The Timer class is available on the .NET Compact Framework and is thus available for both C# and Visual Basic .NET.

Timers are objects that have a Tick method into which developers insert code. The code in the Tick method is executed at regular intervals, which the developer gets to set.

Thus, the Timer is very useful for executing a body of code at specific intervals. The Timer is meant for performing light work at regular intervals, but it is not a good choice for executing code that will take a long time to execute. For such cases a Thread is a far better choice.

The advantages of using Timers on the .NET Compact Framework include these:

  • Easy to set up

  • Easy to control the wakeup interval

  • Implicitly sleep on their own when not executing their code body

  • Easy to enable and disable

In addition to these benefits, the code executing in a Timer can access control objects as long as the thread that owns the Timer also owns the control. Thus, timers can be used as a "poor man's" UI update thread in some circumstances.

Creating a Timer

The easiest way to create a Timer is to use the Smart Device Extensions in Visual Studio .NET to help. Follow these steps to create a Timer :

  1. Select the design view of the form that will own the Timer .

  2. Select the Timer control on the toolbox and drag it to the hidden object viewer. An icon representing the Timer appears. Figure 4.1 shows the design view for the form in the TimerDemo application. In this figure there are two timers that have already been created.

    Figure 4.1. This TimerDemo application has singles and decade timers.

    graphics/04fig01.gif

  3. Double-click the icon for the Timer . Visual Studio responds by creating an empty Tick method for the Timer .

  4. Insert code into the Tick method.

Writing Tick Method Code

The following sample code shows the contents of the Tick method for the Timer named singlesCounter in the TimerDemo sample application:

 
 C# private void singlesTimer_Tick(object sender, System.EventArgs e) {    this.txtSingles.Text = Convert.ToString(m_SinglesCount);    m_SinglesCount = m_SinglesCount + 1; } VB Private Sub singlesTimer_Tick(ByVal sender As System.Object, ByVal e As         System.EventArgs) Handles singlesTimer.Tick    Me.txtSingles.Text = Convert.ToString(m_SinglesCount)    m_SinglesCount = m_SinglesCount + 1 End Sub 

Setting the Tick Interval

The tick interval of a Timer controls how often the code in the Tick method is executed. The value of the tick interval is held in the Timer.Interval property, and it is measured in milliseconds . The property can be set in code simply by assigning a value to the Interval property, for example like so:

 
 C# // Code in the Tick method is executed every 1000 milliseconds myTimer.Interval = 1000; VB ' Code in the Tick method is executed every 1000 milliseconds myTimer.Interval = 1000 

You can also set the Interval property of the timer from within Visual Studio. To do so, select the icon representing the Timer in the designer and then press F4 to update its properties. Choose the desired value for the Interval property.

There is a very important potential pitfall when using Timer objects. If you use a Timer to handle a queue of data objects, it is possible for the queue to become populated faster than the Timer can process them. If this happens, your application's reaction times can appear slow, and you can even start to see exceptions if the queue overflows.

When the amount of time needed to execute the Timer.Tick method is longer than the interval, a variety of bad things can happen. If your code expects the timer to process a set of data at each interval but the timer doesn't get to it all in time, your application could appear to lose data objects. Alternately, your application could fail in a seemingly random manner, because the failure is timing sensitive. These kinds of bugs are among the worst to reproduce and diagnose, and it is a bad idea to work with a design that invites such troubles.

These two problems reinforce the notion that timers are not a good choice for executing complex code. They are wonderfully easy to set up and use for light "housekeeping"-type tasks , but if your Tick method code starts to get long, start thinking about using threads instead.

Suspending and Resuming Timers

The Timer.Enabled property controls whether the Timer wakes up and executes its Tick method. To suspend a Timer , set its Enabled Property to false . To resume a Timer , set the property to true . For example, these snippets accomplish this:

 
 C# // suspend the timer myTimer.Enabled = false; // Do some work with the timer suspended // enable the timer again myTimer.Enabled = true; VB ' suspend the timer myTimer.Enabled = False ' Do some work with the timer suspended ' enable the timer again myTimer.Enabled = True 

Manipulating Timers with a Sample Application

The TimerDemo sample application is located in the folder \SampleApplications\Chapter4 . There are separate directories for the C# and Visual Basic versions.

This sample application demonstrates creating Timer objects, starting them, and toggling their Enabled properties so that individual Timer objects can be suspended and resumed. There are two Timer instances in the project, one to count off numbers individually and another to count off numbers by tens.



Microsoft.NET Compact Framework Kick Start
Microsoft .NET Compact Framework Kick Start
ISBN: 0672325705
EAN: 2147483647
Year: 2003
Pages: 206

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net