Delphi Clinic | C++Builder Gate | Training & Consultancy | Delphi Notes Weblog | Dr.Bob's Webshop |
|
It is that time(r) again!
The borland.jbcl.util package contains a lot of useful classes, but isn't documented very well.
One of those utility classes is the Timer class.
We can use this class to start and stop timers in our applications.
Let's take a look at the Timer class and use it in a simple example application.
Available methods
The Timer class contains two static public methods:
The startTimer is used for starting the timer. This methods takes four parameters:
The stopTimer() method stops the timer. The two parameters are the same as the first two parameters for the startTimer() method.
Example
The following example application uses two timers, who have delays of 1000 milliseconds and 600 milliseconds.
The first timer will be repeated, and the second timer will not be repeated.
When the timer event is fired the application will print a single line to the output.
package com.drbob42.jbjar.tip14; import borland.jbcl.util.Timer; import borland.jbcl.util.TimerClient; public class TimerExample implements TimerClient { private static final int TIMER1 = 0; private static final int TIMER2 = 1; private static final long DELAY1 = 1000; private static final long DELAY2 = 600; public TimerExample() { Timer.startTimer(this, TIMER1, DELAY1, true); Timer.startTimer(this, TIMER2, DELAY2, false); } public static void main(String[] args) { TimerExample timerExample = new TimerExample(); } public void timerEvent(int id) { switch (id) { case TIMER1: System.out.println("Timer 1 - This will be repeated every second"); break; case TIMER2: System.out.println("Timer 2 - Only one time"); break; default: break; } } }