Creating Timers






Creating Timers

All the controls you used in Hour 7 had in common the fact that the user can interact with them. Not all controls have this capabilityor restrictiondepending on how you look at it. Some controls are designed for use only by the developer. One such control is the Open File Dialog control you used in your Picture Viewer application in Hour 1, "Jumping In with Both Feet: A Visual C# 2005 Programming Tour." Another control that's invisible at runtime is the Timer control. The Timer control's sole purpose is to trigger an event at a specified interval of time.

Follow these steps to build a timer example project.

1.
Create a new Windows Application titled Timer Example.

2.
Right-click Form1.cs in the Solution Explorer, choose Rename, and then change the name of the default form to fclsTimerExample.cs.

3.
Set the form's Text property to Timer Example.

4.
Add a new Timer control to your form by double-clicking the Timer item in the toolbox (it's located in the Components toolbox category).

The Timer control is invisible at runtime, so it's added to the gray area at the bottom of the screen rather than placed on the form (see Figure).

1. Invisible-at-runtime controls are shown at the bottom of the designer, not on a form.


Set the properties of the Timer control as follows:

Property

Value

Name

tmrClock

Enabled

True

Interval

1000


You probably noticed that there are very few properties for the Timer control compared to the other controls you've worked with. The most important property of the Timer control is the Interval property. The Interval property determines how often the Timer control fires its Tick event (where you'll be placing code to do something when the designated time elapses). The Interval property is specified in milliseconds, so a setting of 1,000 is equal to 1 second, which is exactly what you set the Interval to for this example. As with many controls, the best way to understand how the Timer control works is to use it. You are now going to create a simple clock using the Timer control and a Label control. The way the clock works is that the Timer control fires its Tick event once every second (because you've set the Interval property to 1,000 milliseconds). Within the Tick event, you update the label's Text property to the current system time.

Add a new label to the form and set its properties as follows:

Property

Value

Name

lblClock

AutoSize

False

BorderStyle

FixedSingle

Location

96,120

Size

100,23

Text

(make blank)

TextAlign

MiddleCenter


The AutoSize property of the label determines whether the label automatically adjusts its size when its Text property changes. Because we're aligning the text to the middle of the control, we don't want it to autosize.

Next, double-click the Timer control to access its Tick event. When a timer is first enabled, it starts counting (in milliseconds) from 0. When the amount of milliseconds specified in the Interval property passes, the Tick event fires, and the timer starts counting from 0 once again. This cycle continues until the timer is disabled (its Enabled property is set to False). Because you've set the Enabled property of the timer to True at design time, it starts counting as soon as the form on which it's placed is loaded. Enter the following statement in the Tick event:

lblClock.Text = DateTime.Now.ToLongTimeString();

The .NET Framework provides date/time functionality in the System namespace. The Now property of the DateTime class returns the current time. Using the ToLongTimeString method returns the time as a string with a format of hh:mm:ss. This code causes the Text property of the label to show the current time of day, updated once a second. Press F5 to run the project now, and you'll see the Label control acting as a clock, updating the time once every second (see Figure).

2. Timers make it easy to execute code at specified intervals.


Stop the running project now and save your work.

Timers are powerful, but you must take care not to overuse them. For a timer to work, Windows must be aware of the timer and must constantly compare the current internal clock to the interval of the timer. It does all this so that it can notify the timer at the appropriate time to execute its Tick event. In other words, timers take system resources. This isn't a problem for an application that uses a few timers, but I wouldn't overload an application with a dozen timers unless I had no other choice (and there's almost always another choice).



 Python   SQL   Java   php   Perl 
 game development   web development   internet   *nix   graphics   hardware 
 telecommunications   C++ 
 Flash   Active Directory   Windows