The Microsoft 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.
Multitasking Sometimes it is more efficient for a program to return control to Windows as quickly as possible after processing a message. If a program must do a large amount of processing, it can divide the job into smaller pieces and process each piece upon receipt of a WM_TIMER message.
The timer is as a guarantee that a program can regain control sometime in the future afterexiting the window procedure.
Maintaining an updated status report A program can use the timer to display "real-time" updates of continuously changing information, such as a display of system resources or the progress of a certain task.
Implementing an " autosave " feature The timer can prompt a Windows program to save a user's work to disk whenever a specified period of time has elapsed.
Terminating "dem o " versions of programs Some demonstration versions of programs are designed to t erminate, say, 30 minutes after they begin. The timer can signal such applications when the time is up.
Pacing movement Graphical objects in a game or successive displays in a computer-assisted instruction program might need to proceed at a set rate. Using the timer eliminates the inconsistencies that might result from variations in microprocessor speed.
Multimedia Progr ams that play CD audio, sound, or music often let the audio data play in the background . A program can use the timer to periodically determine how much of the audio has played and to coordinate on-screen visual information.
SetTimer function : You can allocate a timer for your Windows program by calling it. SetTimer includes an unsigned integer argument specifying a time-out interval that can range (in theory) from 1 msec (millisecond) to 50 days(=4,294,967,295 msec). The value indicates the rate at which Windows sends your program WM_TIMER messages.
KillTimer function: It stops the timer messages. The KillTimer call purges the message queue of any pending WM_TIMER messages. No WM_TIMER messages can be received following a KillTimer call.
WM_TIMER : It is a normal message like any other window message that are sent to a window periodically through a message queue of that window.
It is a low priority message and a program will receive them only if there is no other message in the message queue.
Windows does not keep loading up the message queue with multiple WM_TIMER messages. Instead, Windows combines multiple WM_TIMER messages in the message queue into a single message. Therefore, the application won't get a bunch of them at once, although it might get two WM_TIMER messages in quick succession.
The WM_TIMER messages are not asynchronous. The WM_TIMER messages are placed in the normal queue.
Again using the timer basics her we have two steps:
1.call the SetTimer from WinMain function/ or processing the WM_CREATE message.
2.call the KillTimer on exiting WinMain/ or processing the WM_DESTROY message.
There are 3 methods to do so depending upon the arguments to the SetTimer function.
This method, the easiest, causes Windows to send WM_TIMER messages to the normal window procedure of the application.
The SetTimer call looks like this:
SetTimer (hwnd, 1, uiMsecInterval, NULL) ;
hwnd:handle to the window;
1 : Timer ID
uiMsecInterval: 32 bit intger variable for a time interval
You can stop the WM_TIMER messages at any time (even while processing a WM_TIMER message) by calling
KillTimer (hwnd, 1) ;
hwnd:handle to the window
1 :The same timer ID
With this second method, you can direct Windows to send the timer messages to another function within your program.
The function that receives these timer messages is termed a "call-back" function. This is a function in your program that is called from Windows. You tell Windows the address of this function, and Windows later calls the function.
the fourth argument to SetTimer is instead the address of the
KillTimer (hwnd, 1) ;
This remains the same.
The third method of setting the timer is similar to the second method, except that the hwnd parameter to SetTimer is set to NULL and the second parameter (normally the timer ID) is ignored. Instead, the function returns a timer ID.
It might come in handy if you do a lot of SetTimer calls at different times in your program and don't want to keep track of which timer IDs you've already used.