Chapter 14: Examples of Programs Using the Timer

Overview

The timer is one of the most powerful instruments provided by the operating system and used to solve various problems. You became acquainted with the timer when considering console applications. There, I used the timeSetEvent and timeKillEvent functions. These functions are convenient for console applications. In graphic user interface applications, the SetTimer and KillTimer functions are used more frequently. A feature of the timer created by the SetTimer function is that the WM_TIMER message, which the system starts to send to the application after execution of the SetTimer function, arrives with all other messages. Consequently, the interval between two sequential WM_TIMER messages can vary slightly. However, in most cases, this isn't important.

The timer message has another feature. If the system sends the timer message to the application, and the previous message is still waiting in the message queue, the system joins these two messages. Thus, this forced wait doesn't result in the arrival of several timer messages in succession to the application.

The list of tasks that can be carried out using the timer includes the following:

  • Tracking the time (seconds counter, watch, etc.) Periodicity variation is of no importance because, when the message arrives, the time can be tracked by calling the function for getting the system time.

  • Implementing multitasking It is possible to set several timers simultaneously to different functions. As a result, the functions will alternate periodically. More detailed information about multitasking will be provided in Chapter 15.

  • Periodic refreshing of the displayed information.

  • Auto saving This function is especially useful for editors.

  • Setting the rate of changing specific objects displayed on the screen.

  • Animation When a timer message arrives, the graphics displayed on the screen or in the window are refreshed. This creates the animation effect.

Consider how to work with the SetTimer function. Here are the parameters of this function:

  • First parameter Descriptor of the window, with which the timer is associated. If this parameter is set to NULL , then the second parameter will be ignored.

  • Second parameter Defines the timer identifier.

  • Third parameter Specifies the periodicity interval for the WM_TIMER message.

  • Fourth parameter Defines the address of the function, to which the WM_TIMER message will arrive . If this parameter is set to NULL , the message will arrive to the window function.

If the function has completed successfully, it returns the timer identifier, which, naturally, will match the second parameter if it is other than NULL . In the case of failure, the function returns zero.

Thus, the function can be called using the following three methods :

  • The window descriptor is specified, and the fourth parameter is set to 0.

  • The window descriptor is specified, and the fourth parameter defines the function, to which the WM_TIMER message will arrive.

  • The window descriptor is set to NULL , and the fourth parameter defines the function, to which the WM_TIMER message will arrive. Timer identifier in this case will be determined by the value returned by the function.

The function, to which the WM_TIMER message arrives, has the following parameters:

  • First parameter Descriptor of the window to which the timer is associated

  • Second parameter The WM_TIMER message

  • Third parameter The timer identifier

  • Fourth parameter Time interval in milliseconds that has elapsed since Windows startup

The KillTimer function deletes the existing timer. It accepts the following parameters:

  • First parameter Window descriptor

  • Second parameter Timer identifier



The Assembly Programming Master Book
The Assembly Programming Master Book
ISBN: 8170088178
EAN: 2147483647
Year: 2004
Pages: 140
Authors: Vlad Pirogov

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net