From Xojo Documentation
Supported PlatformsProject Types: Desktop, Web, Console Platforms: macOS, Windows, Linux
- For web apps, see WebTimer.
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.
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 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.
Using Timer with AddHandler
When creating a Timer in code, the timer starts when you set the mode to either ModeSingle or ModeMultiple. Refer to the AddHandler command for an example on how to use a method to handle the Timer's Action 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 Action 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. But due to operating system limitations, a Thread cannot directly access or modify the user interface. Doing so raises a ThreadAccessingUIException. This can be a problem because it is often helpful for the Thread to provide information about its progress to the user interface.
The correct technique is to instead have a Timer periodically check the progress of the Thread (perhaps by checking a property on the Thread). Since the Timer runs on the main thread, it is able to update the user interface.
Example projects: Desktop/UpdatatingUIFromThread
Also refer to the Task class for a Thread subclass that has Timer updates built-in to it.
A Timer can be used to monitor keydown events. The following code in the Action event of a Timer (Mode = Multiple, Period = 100) detects whether the Up, Down, Left, or Right arrow keys are pressed.
Label1.Text = "left arrow key"
If Keyboard.AsyncKeyDown(124) Then
Label1.Text = "right arrow key"
If Keyboard.AsyncKeyDown(125) Then
Label1.Text = "down arrow key"
If Keyboard.AsyncKeyDown(126) Then
Label1.Text = "Up arrow key"