Character mode Vermont Views for DOS had the advantage that DOS was a single-tasking operating system and that the amount of CPU consumed by the application during idle states did not matter, so calling a key loop function continuously while waiting for keystrokes was perfectly acceptable. Terminal-based Vermont Views could do the same thing, but had to suffer the extra CPU consumption on a system that was designed to support multiple users and multiple tasks.
Vermont Views for Windows provides a solution to both of those cases – Timers. The new functions start_timer(), stop_timer() and se_timerfp() solve the problem of CPU consumption by using the built-in Windows timers and consequently don't consume the CPU until Windows calls the supplied callback function. This means that it is possible to display a running clock, refresh the screen at regular intervals or monitor a communications port without a major impact on the system.
Vermont Views for Windows provides a new event, AC_TIMEREVENT to provide the link between the Windows timer and the execution of the callback event. This event is installed in the system event table SYSETP and the callback function must conform to the requirements of a system event table function. See event Handling in the Vermont Views users guide for more information about adding and handling events.
When your callback function is called, it is provided the following details as part of the EVENTPTR structure that is passed to it:
| AC_TIMEREVENT parameters: |
| eventp->type= AC_TIMEREVENT;// event type |
| eventp->lmsg= timerID; //set in start_timer() |
| eventp->target= FLDLOOKUP *; // FLDLOOKUP structure |
| eventp->x = 0; |
| eventp->y = 0; |
| eventp->when= GetTickCount(); //system tickcount |
|
To Start a Timer: |
| The function start_timer() function is a wrapper for the SetTimer() Windows API function. Once a timer is set, the current window's processing function will get an AC_TIMEREVENT event at EACH expiration of the timeout interval specified in the milliseconds argument of start_timer(). |
| Installing the callback function in the event table: |
| Use the function se_timerfp() to place your timer function in the System Event Table. |
| To stop a timer: |
| Call stop_timer() with the timerID used in start_timer(). |
Example:
| ... |
| extern FORMPTR mainFmp; |
| // Set timer event handler |
| EVENTFP oldHandler = se_timerfp(timerHandler); |
| // Start the timer |
| start_timer(333, 500); |
| ... |
| // Display the time |
| int timerHandler(KEYCTRLPTR kcp, EVENTPTR eventp) |
| { |
| switch(eventp->lmsg) |
| { |
| case 333: |
| { |
| char tBuff[64]; |
| SYSTEMTIME systemTime; |
| // Get time from Windows |
| GetLocalTime(&systemTime); |
| // Let VV format it for display |
| time_frnums(tBuff, systemTime.wSecond, systemTime.wMinute,
systemTime.wHour, "HH:MM:SS"); |
| // Draw it on the screen |
| if (mainFmp && fm_isup(mainFmp)) |
| v_stattpl(18, 2, tBuff, LURGENT, STATT, mainFmp->wnp); |
| } |
| } |
| return(1); |
| } |
| ... |
| // stop the timer |
| stop_timer(333); |
| ... |
For additional information about timers in Windows, see the Timers section of the Win32 Programmer's Reference included with your compiler.
The VVWinDemo example program in the \samples\vvwindemo directory contains a form that demonstrates the use of timers in the sysmetrics.c module.