Programming with Timer Controls
- Updated2023-02-21
- 3 minute(s) read
Programming with Timer Controls
Use timer controls to generate events at a predefined time interval. These events are sent to a callback function that you define.
The number of timer controls you can use is unlimited, but timer callbacks are called sequentially. If two or more timers have identical time intervals, their callbacks are called in an undefined, but consistent order. Use the following functions to manage timer controls:
- SuspendTimerCallbacks stops calls to all the timer control callbacks until you call ResumeTimerCallbacks to restore timer control activity. These functions do not affect the interval schedules or set the enabled/disabled state of individual timer controls, but rather activate and deactivate a blanket suspension over all timer callbacks. If you want to enable or disable an individual timer, use SetCtrlAttribute with the ATTR_ENABLED attribute.
- ResetTimer resets the interval start times of individual timer controls, all timer controls on a panel, or all timer controls on all panels.
Using Timer Callbacks
The timer callback has the same prototype as other control callbacks.
int CVICALLBACK timerfunc (int panel, int control, int event, void *callbackData, int eventData1, int eventData2)
eventData1 is a pointer to a double that represents the current time in seconds. The time base is the same as that used by the Timer function in the Utility Library. eventData2 is a pointer to a double that represents the time elapsed since the last EVENT_TIMER_TICK, in seconds.
When the callback function is called at the end of a timer interval, the event that is generated is EVENT_TIMER_TICK.
![]() |
Note National Instruments recommends that you use the GetTimerTickData function to obtain the time of the current EVENT_TIMER_TICK callback and the time elapsed since the last EVENT_TIMER_TICK callback instead of using the values returned in the eventData1 and eventData2 parameters. GetTimerTickData is the only way to get the time values from an EVENT_TIMER_TICK event in a 64-bit executable. If you are creating a 32-bit only application, you can use the values returned by eventData1 and eventData2. |
Details of Timer Control Operations
After a timer control is created or loaded, the timer does not start until a call is made to RunUserInterface, GetUserEvent, or ProcessSystemEvents. This ensures that you can create or load several timer controls and have them start at the same time.
The following actions do not affect the timer interval schedules:
- Calls to SuspendTimerCallbacks
- Calls to ResumeTimerCallbacks
- Changes to the value of ATTR_ENABLED
To reset timer interval schedules, call ResetTimer. To change the length of the timer intervals, use the ATTR_INTERVAL attribute. Also, when you change the ATTR_INTERVAL value, you cause the interval schedule for the timer control to be reset.
The time intervals you specify are minimum values. System activity, including processing of user callbacks, can cause timer callbacks to be late or skipped. If you need real-time response, you should allow your program to process events during any time-consuming operations.
Timer callbacks are called only while a call to RunUserInterface, GetUserEvent, or ProcessSystemEvents is in effect. A call to RunUserInterface, GetUserEvent, or ProcessSystemEvents does not alter interval schedules already in effect, but might trigger an overdue callback. If your program calls one of these functions and the time since the last callback is greater than the ATTR_INTERVAL value for a timer control, the callback is called. Only one such overdue callback is called, even when more than one interval has elapsed. The calling of an overdue callback does not cause rescheduling of the next interval. Thus, after an overdue callback occurs, the next regularly scheduled callback might take place sooner than the time between intervals that ATTR_INTERVAL specifies.
A call to ResumeTimerCallbacks or setting ATTR_ENABLED to TRUE does not trigger overdue callbacks.