From Xojo Documentation

You are currently browsing the old Xojo documentation site. Please visit the new Xojo documentation site!

For web apps, see WebTimer.

Class (inherits from Object)

A Timer is an object that can run code after a specified period of time has elapsed or at repeated time intervals. If added to a layout, it is not visible in the UI since it is not a control.

Action Run
Enabled Period RunMode
AddActionNotificationReceiver RemoveActionNotificationReceiver Reset
Shared Methods
CallLater CancelCallLater
Delegate Methods
TimerCallLater TimerCallLaterWithValue


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

Timer code runs in the main thread, the same as all other non-Thread code. This means a Timer cannot run if other code in the main thread is running. If you have a long-running process in the main thread, it could prevent your Timer from running at the intervals specified.

Although the Timer appears in the list of Built-in controls in the Library, 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 RunMode property controls the interval used to execute the Timer's Action event handler. If the RunMode is not Timer.RunModes.Off, the Timer waits for the Period to pass before executing the Run event handler. If the RunMode is set to Timer.RunModes.Off at Runtime, the Timer will immediately cease waiting and the Run event handler will no longer be executed.

The Timer will continue to execute its Run event handler (assuming the RunMode property is not set to Timer.RunModes.Off) 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 Run 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.

Timers in Web Projects

Timers in web projects will execute outside of all sessions. If you need a Timer that knows the session in which it is executing, use a WebTimer instead.

Using Timer with AddHandler

When creating a Timer in code, the timer starts when you set the mode to either Timer.RunModes.Single or Timer.RunModes.Multiple. Refer to the AddHandler command for an example on how to use a method to handle the Timer's Run event.

Limitations on Microsoft Windows

On Windows, the standard system timer is used which has a default resolution of about 15ms.

Source: Stack Overflow

Updating the User Interface using a Timer

Because a Timer (and thus its Run event handler) always runs in the main thread, it can be used to update the user interface for long-running processes within threads.

Typically you put long-running processes within a Thread to keep the user interface responsive. See the Thread class for information on how to properly update your application's user interface from a Thread.

Sample Code

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

If Keyboard.AsyncKeyDown(123) Then
Label1.Text = "left arrow key"
End If
If Keyboard.AsyncKeyDown(124) Then
Label1.Text = "right arrow key"
End If
If Keyboard.AsyncKeyDown(125) Then
Label1.Text = "down arrow key"
End If
If Keyboard.AsyncKeyDown(126) Then
Label1.Text = "Up arrow key"
End If

See Also

Object, Thread, WebTimer, Xojo.Core.Timer classes; AddHandler command.