Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
How many timers are TOO MANY?
Message
From
21/07/2004 14:30:01
Hilmar Zonneveld
Independent Consultant
Cochabamba, Bolivia
 
General information
Forum:
Visual FoxPro
Category:
Other
Miscellaneous
Thread ID:
00926585
Message ID:
00926692
Views:
16
This message has been marked as the solution to the initial question of the thread.
>Hmmm...Here's my concern. Let's say timer 1 should fire every 10 seconds and timer 2 every 1 second. If I put these in the same timer control and the code from timer 1 executes and it takes 3 seconds then the code from timer 2 won't fire until timer 1 code is done. Now multiply this by 8 timers. Does that make sense??

Of course, this is a valid concern, and it has to be accounted for. If a Timer fires every 10 seconds and takes more than 10 seconds to execute - or even anything approaching 10 seconds - then a redesign seems appropriate. For instance, execute the timer less often. This is the case, whether you use traditional Timers, or a Timer-wrapper (as we might call the object under discussion: all Timers combined into one).

On the other hand, the Timer-wrapper should be as efficient as possible. To accomodate the possibility of many Timers, you should have the data in a cursor, for fast searching, IMO.

My previous idea would have been: fire the Timer-wrapper every 10 seconds. Here is a redesign, which should be more efficient. The idea is to calculate the Interval property every time, adjusting it to the time of the next event. The implementation would be something like this:

At the beginning, check the cursor of timer-events:
* Method SetNextTimer
select TimerEvents
set order to "FireAt"
go top
and store (FireAt - DateTime()) * 1000 (milliseconds) to the Interval property of your single Timer.

Every time the Timer() Event fires, process the corresponding event, and invoke method SetNextTimer() again. Also, repeat the calculation every time the .AddTimerEvent() method is called.

BTW, if your Timer-wrapper is in a Session class, it can have its own datasession - isolated from other parts of the program. The Session class is available in VFP6 SP3 or later.

So far, then, my idea is as follows.

Class based on Session Class, or Form class (Form class allows visual design).

Add a Timer Class to it.

(Alternative: just use a Timer object, and have the data in the default datasession.)

Create the cursor TimerEvents, with fields:
FireAt T && When should it fire
Expression M && Expression that should be evaluated at that moment
Method Init(): sets up the cursor.

Method AddTimerEvent(): Adds a Timer event. Invokes SetNextTimer().

Method SetNextTimer(): Adjusts the Interval property.

Method Timer.Timer(): Executes the desired code, as specified in the cursor. Sees if there are other overdue events, and executes them. Invokes SetNextTimer().
Difference in opinions hath cost many millions of lives: for instance, whether flesh be bread, or bread be flesh; whether whistling be a vice or a virtue; whether it be better to kiss a post, or throw it into the fire... (from Gulliver's Travels)
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform