The Windows timer is an input device that periodically notifies an application when a specified interval of time has elapsed. Your program tells Windows the interval, in effect saying, for example, ”Give me a nudge every 10 seconds.“ Windows then sends your program recurrent WM_TIMER messages to signal the intervals.
At first, the Windows timer may seem a less important input device than the keyboard or mouse, and certainly it is for many applications. But the timer is more useful than you may think, and not only for programs (like the Windows CLOCK) that keep time. The CALENDAR, CONTROL, REVERSI, SPOOLER, TERMINAL, and WRITE programs supplied with Windows also use the timer. Here are some uses for the Windows timer, some obvious and some perhaps not so obvious:
Keeping time—Both the CLOCK and CONTROL programs that come with Windows display the current time. The timer tells the programs when to update the clock. The DIGCLOCK program, described later in this chapter, uses the timer to display a digital clock.
Maintaining an updated status report—The FREEMEM program, shown in this chapter, uses the timer to display available memory in Windows. The display is updated every second.
Waking up—The Windows CALENDAR program uses the timer to trigger a preset alarm.
Multitasking—Windows is a nonpreemptive multitasking environment, and it is important that programs return control to Windows as quickly as possible. If a program must do a large amount of processing, it can divide the job into smaller pieces and process each piece on receipt of a WM_TIMER message.
Implementing an ”autosave“ feature—The timer can prompt a Windows program to save a user's work to disk whenever a specified amount of time has elapsed.
Pacing movement—Graphical objects in a game or successive displays in a computer-assisted instruction program may need to proceed at a set rate. Using the timer eliminates the inconsistencies that might result from variations in microprocessor speed.
Terminating demonstration versions of programs—Some demonstration versions of programs are designed to terminate, say, 30 minutes after they begin. The timer can signal such applications when the time is up.
Using serial or parallel communications—Unlike most other input devices in Windows, serial or parallel communications ports do not generate messages. Rather, these programs must poll for input, and the timer can tell them when to do so. (An alternative to using the timer for polling involves a message loop built around the PeekMessage call. This technique is discussed in Chapter 15.)
This chapter also explores topics that extend beyond the timer to other areas of Windows programming. Foremost among these topics is that of ”call-back“ functions. To the uninitiated, these important functions might seem to work in mysterious ways, and the timer is not the only place you will encounter them. This chapter also discusses what to do when a program cannot gain access to a timer—a problem that occurs because Windows maintains only a limited number of timers. Solving this problem is fundamental to working with the Windows timer, but the method presented here can also be applied to error handling in other programs. Finally, the sample programs shown here deal with such decidedly nontimer issues as Windows' use of color, using a type of window known as a ”popup,“ forcing an application to be loaded as an icon, obtaining the amount of free memory available in Windows, using floating-point mathematics in your Windows programs, and accessing the WIN.INI file to obtain information about international time and date formats.