CodeMorphis
Joined: 02 Dec 2003 Posts: 252
|
Posted: Thu May 19, 2005 5:32 pm Post subject: Programming Tip #16: The Timer Component |
|
|
Programming Tip #16: The Timer Component
Often in a Windows program, there is a need to execute tasks on regular intervals. You may want to move the timeline slider of a video display every second, or advance the time of a digital clock, or even fire off an email with status information every hour. The Timer component allows you to control logic on time-based intervals.
The Timer component can be found in the Windows tab of the component tray and has 4 services:
- Create: Allows you to create a Timer object with a timer interval.
- Set interval: Allows you to change the time interval on which the timer operates.
- Start: Starts the timer. When the timer is on, it generates a TimerEvent event on the current interval.
- Stop: Stops the timer. When the timer is off, no TimerEvent events are generated.
To use a timer, we first have to set it up. Here's a typical initialization sequence:
After displaying our windows form, we create the timer using the "Create" service. Here are the arguments that we are using for this call:
The "Timer ID" is simply an integer number that identifies our timer, since our program can have many timers running simultaneously, if we want. A valid ID is a number greater than 0. The timer needs to be associated to a Windows form so we have selected our form name ("Win Form 1") in input data port 1, i.e. the second argument called "Windows Form". The third argument is the "Timer interval", which indicates the frequency that the timer produces events. This value is given in milliseconds and by default is 1000, meaning that the timer will produce a timer event every second.
The above sequence of calls completes the initialization of the timer and we are ready to use it in our program. Here's a simple program that uses the timer that we just created:
We have completed the program by adding a Windows Event component to create an event loop. We have also added a Console Print component that will write a simple echo text every time the timer produces an event, i.e. every second.
We just need to edit the Windows Event component's properties to tell it that we want it to trap timer events (by default the Windows Event component only traps left mouse button up events, which is the most common event needed). By double clicking on the Windows Event component, we can edit the property set:
Note that we have enabled the "TimerEvent" property. Now every second, the timer component will generate a "TimerEvent" which will appear in output data port 0 of the Windows Event component as the "Event Type" descriptor. The timer's ID, which is 1, will also appear in output data port 2 as the "UI component name". (NOTE: We also disabled the "LeftMouseButtonUpEvent" so that user mouse clicks in the Windows form are ignored).
If you now run the program, you will see the text "Tick tock!" appear every second in the console output area. We set this text in input data port 0 of the Console Print component.
You can obtain this program here: http://www.codemorphis.com/articles/tip16/simple_timer_prog.vpd.
Because our program only has one event of interest, namely the timer event, we didn't need to add any logic tests to know what occurred whenever the Windows Event component trapped an event. Usually, however, your program will do many more things and so it is necessary to add some tests.
Below we have a modified version of our program, which can be downloaded at http://www.codemorphis.com/articles/tip16/simple_timer_prog2.vpd:
In this program, we have added a second Timer component, which has the name "Timer 2". This timer has an ID of 2 and a timer interval of 2000 milliseconds, or 2 seconds. After creating the two timers, they are both started before entering the Windows Event loop. Because there are now two timers, we need to test for them when the Windows Event component traps the TimerEvent event, so that we can know what appropriate action to take. The relevant information is the "UI component name" field which comes through output data port 2 of the Windows Event component. This information tells us the timer ID that has generated the TimerEvent event.
This test is made with the Switch component, which tests the event-generating timer ID against the values "1" and "2", which we have stored in the Switch's input data ports 1 and 2, respectively.
If the timer ID is 1 then we move to "Con Print 1" which outputs its input argument "Tick tock!". If the timer ID is 2 then we move to "Con Print 2" which outputs its argument (which we have set as "The mouse ran up the clock"). If we run the program this is what we get in the console output area:
![](http://www.codemorphis.com/articles/tip16/prog_output.jpg) |
|