the timer

12
Gaurav Paliwal(007) Deepak Jain(054) Mayank Varshney(056) Amarjeet Chauhan(067)

Upload: gaurav-paliwal

Post on 12-May-2015

1.010 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: The Timer

Gaurav Paliwal(007) Deepak Jain(054) Mayank

Varshney(056) Amarjeet

Chauhan(067)

Page 2: The Timer

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.

Page 3: The Timer

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.

Page 4: The Timer

Terminating "demo" 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. 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 Programs 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.

Page 5: The Timer

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.

Page 6: The Timer

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.

Page 7: The Timer

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.

Page 8: The Timer

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 IDuiMsecInterval: 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 callingKillTimer (hwnd, 1) ;

hwnd:handle to the window1 :The same timer ID

Page 9: The Timer

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 SetTimer call looks like this:

SetTimer (hwnd, iTimerID, iMsecInterval, TimerProc) ;

the fourth argument to SetTimer is instead the address of the

call-back function. KillTimer (hwnd, 1) ;

This remains the same.

Page 10: The Timer

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.

The SetTimer call looks like this:

iTimerID = SetTimer (NULL, 0, wMsecInterval, TimerProc) ;

The iTimerID returned from SetTimer will be 0 in the rare event that no timer is available.

The first parameter to KillTimer (usually the window handle) must also be NULL. The timer ID must be the value returned from SetTimer.

KillTimer (NULL, iTimerID) ;

Page 11: The Timer

GetLocalTime function reports the local time,based on the time zone of the location of the computer.

GetSystemTime function reports the current Coordinated Universal Time (UTC), which is roughly the same as Greenwich mean time the date and time at Greenwich, England.

GetLocalTime, which takes as a single argument the SYSTEMTIME structure, defined in WINBASE.H like so:

typedef struct _SYSTEMTIME{WORD wYear ;WORD wMonth ;WORD wDayOfWeek ;WORD wDay ;WORD wHour ;WORD wMinute ;WORD wSecond ;WORD wMilliseconds ;}SYSTEMTIME, * PSYSTEMTIME ; As is obvious, the SYSTEMTIME structure encodes the date as well as the time.

Page 12: The Timer