Table of Contents
20.3.0 Timer
The class Timer (gb) implements a Timer object. A Timer object is to be understood as a clock that triggers events with each clock pulse. The time between each clock pulse is determined by the value of the Delay property (clock pulse time in milliseconds).
20.3.0.1 Properties
The Timer class has only two properties:
| Property | Data type | Description |
|---|---|---|
| Delay | Integer | Returns the number of milliseconds between timer events or sets the number of milliseconds between clocks. |
| Enabled | Boolean | Starts the timer with Timer.Enabled = True or determines whether the timer is active. A deactivated timer can no longer trigger timer events. |
Table 20.3.0.1.1 : Properties of the Timer class
20.3.0.2 Methods
The Timer class has three methods, the description of which can be found in the following table:
| Method | Description |
|---|---|
| Start | The timer is started. This has the same effect as Timer.Enabled = True. |
| Stop | The timer is stopped. This has the same effect as Timer.Enabled = False. |
| Trigger() | Timer.Trigger() triggers the Timer_Timer() event on the next iteration of the event loop. This method is useful for executing the event handler “later” - at least after the current stack of function calls or at the next explicit call to WAIT. |
Table 20.3.0.2.1 : Methods of the class Timer
20.3.0.3 Event
There is exactly one event for the class Timer → Timer_Timer(). This event is triggered every time the cycle time specified via Timer.Delay has elapsed.
Note that the cycle time is at least Timer.Delay. There is generally no guarantee - depending on the hardware used and and the operating system and its configuration - that the event handler will be executed “close in time”.
20.3.0.4 Fields of Application for the Class Timer
The following is a compilation of selected fields of application for Timer objects from various demonstration projects in the online book:
- Clocks (analogue, digital, binary),
- stopwatches,
- stopwatches,
- count-down counters (date or time),
- TimeOut for time-critical routines,
- Triggering of actions once, several times (asynchronous) or periodically → measured values, status acquisition, automatic storage,
- Repeat functions for button,
- flashing displays of text or of symbols (single or multi-coloured) with adjustable frequency.
This concept has proven successful when using a timer:
- Determining whether the use of a timer allows the specified goal to be achieved.
- Determine clock time and set the Timer.Delay property with a suitable start value.
- Determine in which procedures the timer is started and stopped.
- Specify whether the 1st timer event should be triggered immediately after the timer start with the Timer.Trigger() method or only after the 1st clock pulse.
- Define exactly what should happen when the Timer_Timer() event is triggered.
20.3.0.5 Timer function Timer()
The timer function Timer() - which is unrelated to the class Timer - specifies the number of seconds that have elapsed since the programme start. The function value is of the float data type. The function uses the system clock.
With the following source code excerpt
Dim fZeitdifferenz as Float fZeitdifferenz = Timer Print "Sekunden seit Programm-Start = ";;fZeitdifferenz;;" Sekunden"
… the number of seconds elapsed since the programme start is displayed in the console of the Gambas IDE:
Zeit seit Programm-Start = 17,8296689987183 Sekunden
The function can be used, for example, to measure runtime when comparing search or sort algorithms or for selected, time-intensive procedures.
