Timer

From Xojo Documentation

Revision as of 18:56, 19 November 2009 by WikiSysop (talk) (1 revision)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
You are currently browsing the old Xojo documentation site. Please visit the new Xojo documentation site!


Description

A Timer is an object that can execute code after a specified period of time or at a repeated interval. If added to a window via the Window Editor, it is not visible in the built application since it is not a control.


Super Class

Object

Because it is subclassed from Object rather than RectControl, you can instantiate it via code with the New operator.


Events

Name Parameters Description
Action The Action event handler contains the code that will execute after the specified Period and in the specified Mode.


Properties

Name Type Description
Enabled
Introduced 5.0
Boolean Enables you to turn the Timer on or off.

When Enabled is True, the Timer is on. Enabled is set to True when you instantiate a Timer. If you turn the Timer off and then turn it on, it resets itself to the start of the period.

Index
Integer The number of the control when it is part of a control array.
Left
Integer The left side of the control in local coordinates (relative to the window).
Mode Integer The interval at which the Action event will be executed.

The following class constants are available: ModeOff (0) - Off, ModeSingle (1) - Single, ModeMultiple (2) - Multiple For example: timer1.Mode=Timer.ModeSingle The default is 2, Multiple.

Period Integer The time (in milliseconds) between executions.

Periods of less than or equal to zero default to a value of 1 millisecond. The default value is 1000. The rate that a Timer can actually fire depends on the speed of the host computer, the operating system, and other tasks the computer is doing. It is possible to set Period to a value that cannot be achieved by the computer that is running the application.

Top
Integer The top of the control in local coordinates (relative to the window).


Methods

Name Parameters Description
Reset
Introduced 5.0
Resets the Timer and restarts it.


Class Constants

The following class constants can be used to set the mode the Timer is using.

Class Constants Description
ModeOff The timer is off.
ModeSingle The timer will fire once.
ModeMultiple The timer will fire repeatedly (the default).


Notes

Although the Timer appears in the list of Built-in controls in the Window Editor, this is done only as a convenience to programmers. In terms of the object hierarchy, the Timer is not a control. It is subclassed from Object. This means you can create Timers in your code via the New operator, just as with other objects.

The Mode property controls the interval used to execute the Timer's Action event handler. If the Mode is not 0 (ModeOff), the Timer waits for the Period to pass before executing the Action event handler. If the Mode is set to ModeOff at Runtime, the Timer will immediately cease waiting and the Action event handler will no longer be executed.

The Timer will continue to execute its Action event handler (assuming the Mode property is not set to ModeOff) regardless of whether the window is frontmost or not. The visibility of the window also has no impact on the execution of a Timer's Action event handler. The Timer has been designed so that it yields time to other applications running on the computer so as not to bog down the machine.


Examples

A Timer can be used to monitor keydown events. The following code in the Action event of a Timer detects whether the Up, Down, Left, or Right arrow keys are pressed.

If Keyboard.AsyncKeyDown(123) then
StaticText1.text="left arrow key"
end if
If Keyboard.AsyncKeyDown(124) then
StaticText1.text="right arrow key"
end if
If Keyboard.AsyncKeyDown(125) then
StaticText1.text="down arrow key"
end if
If StaticText1.AsyncKeyDown(126) then
 StaticText1.text="Up arrow key"
end if


See Also

Object class.