Timers
Timers are a familiar entity to Visual Basic programmers, as they have been part of that language for many
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
The advantages of using Timers on the .NET Compact Framework include these:
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
Writing Tick Method CodeThe 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
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
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
These two problems
Suspending and Resuming TimersThe 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 ApplicationThe TimerDemo sample application is located in the folder \SampleApplications\Chapter4 . There are separate directories for the C# and Visual Basic versions.
This sample application
|