Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2007-05

Timers in FPC

10 messages · 5 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Graeme Geldenhuys

Hi,

What timers classes or components does FPC have? For example something that can fire every 200ms or ever 2 minutes, etc... I'm not worried about high precision timers etc... Being out by a couple of milliseconds is not a issue.

For example Delphi and Lazarus has a TTimer which does the job for me (if I write VCL or LCL based apps). I need something that doesn't rely on the LCL or VCL, but that could be used safely in a GUI environment like fpGUI.

I guess I'm a bit late in asking, seeing that I already implemented by own thread based timer. I works 100% for what I need. I just wondered if there was something like that built into FPC that I missed. I would like to compare the implementations, or share mine if FPC doesn't have one.

Graeme Geldenhuys General error, hit any user to continue.
Michael Van Canneyt
to Graeme Geldenhuys
Graeme Geldenhuys wrote 11 lines
Hi, What timers classes or components does FPC have? For example something that can fire every 200ms or ever 2 minutes, etc... I'm not worried about high precision timers etc... Being out by a couple of milliseconds is not a issue. For example Delphi and Lazarus has a TTimer which does the job for me (if I write VCL or LCL based apps). I need something that doesn't rely on the LCL or VCL, but that could be used safely in a GUI environment like fpGUI.

There currently isn't, as far as I know.

Show quoted text 6 lines
I guess I'm a bit late in asking, seeing that I already implemented by own thread based timer. I works 100% for what I need. I just wondered if there was something like that built into FPC that I missed. I would like to compare the implementations, or share mine if FPC doesn't have one.

You're welcome to share yours.

Michael.

Graeme Geldenhuys
to Michael Van Canneyt
Show quoted text 9 lines
> I guess I'm a bit late in asking, seeing that I already implemented by > own thread based timer. I works 100% for what I need. I just wondered > if there was something like that built into FPC that I missed. I > would like to compare the implementations, or share mine if FPC > doesn't have one. You're welcome to share yours. Michael.

Hopefully somebody could find this useful or possibly it could find its way into FPC and be the beginnings of a basic timer in FPC. Why must everybody always reinvent the wheel. :-)

See attached file: threadtimer.pas

Usage:
-----------

  FTimer := TFPTimer.Create(nil);
  FTimer.OnTimer      := @FTimerTimer;
  FTimer.Interval     := 500;  // in milliseconds
  FTimer.Enabled      := False;
You can then call functions like:
FTimer.On;
FTimer.Off;
FTimer.Enabled := False;
Graeme Geldenhuys General error, hit any user to continue.
Rick Seiden
to Graeme Geldenhuys

There's a contributed units <http://www.freepascal.org/contrib/db.php3> link on the freepascal.org home page. Perhaps it could go there. Right under it is a Contribute <http://www.freepascal.org/contrib/add.php3> link that allows you to add it to the contributed units database.

I know this isn't adding it to the base RTL stuff, but it's a start for now, no?

Graeme Geldenhuys wrote 36 lines
> > I guess I'm a bit late in asking, seeing that I already implemented by > > own thread based timer. I works 100% for what I need. I just wondered > > if there was something like that built into FPC that I missed. I > > would like to compare the implementations, or share mine if FPC > > doesn't have one. > > You're welcome to share yours. > > Michael. Hopefully somebody could find this useful or possibly it could find its way into FPC and be the beginnings of a basic timer in FPC. Why must everybody always reinvent the wheel. :-) See attached file: threadtimer.pas Usage: ----------- FTimer := TFPTimer.Create(nil); FTimer.OnTimer := @FTimerTimer; FTimer.Interval := 500; // in milliseconds FTimer.Enabled := False; You can then call functions like: FTimer.On; FTimer.Off; FTimer.Enabled := False; ------------------------------------------------------------------------ _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal
If you never have the courage to lose sight of the shore, You'll never know the utter terror of being forever lost at sea
Michael Van Canneyt
to Graeme Geldenhuys: Hopefully somebody could find this useful or possibly it could find its way into FPC and…
Graeme Geldenhuys wrote 14 lines
> > I guess I'm a bit late in asking, seeing that I already implemented by > > own thread based timer. I works 100% for what I need. I just wondered > > if there was something like that built into FPC that I missed. I > > would like to compare the implementations, or share mine if FPC > > doesn't have one. > > You're welcome to share yours. > > Michael. Hopefully somebody could find this useful or possibly it could find its way into FPC and be the beginnings of a basic timer in FPC. Why must everybody always reinvent the wheel. :-)

Thanks. I had a quick look.

Do you mind if I split up the implementation like this:

TTimer - Exposes all needed properties, events. Delegates work to TTimerDriver. TTimerDriver - Abstract class which has 2 methods and a property to do the actual work. TThreadedTimerDriver - TTimerDriver Descendent which uses your implementation to do the actual work.

and then I add this to the FCL as unit fpTimer ?

This would allow the LCL to implement a TTimerDriver descendent which integrates nicely in the LCL. The actually used driver is set through some global class variable. And at the same time there is already a default driver available for all platforms.

Michael.

Show quoted text 22 lines
See attached file: threadtimer.pas Usage: ----------- FTimer := TFPTimer.Create(nil); FTimer.OnTimer := @FTimerTimer; FTimer.Interval := 500; // in milliseconds FTimer.Enabled := False; You can then call functions like: FTimer.On; FTimer.Off; FTimer.Enabled := False; -- Graeme Geldenhuys General error, hit any user to continue.
Graeme Geldenhuys
to Michael Van Canneyt
Michael Van Canneyt wrote 8 lines
Do you mind if I split up the implementation like this: TTimer - Exposes all needed properties, events. Delegates work to TTimerDriver. TTimerDriver - Abstract class which has 2 methods and a property to do the actual work. TThreadedTimerDriver - TTimerDriver Descendent which uses your implementation to do the actual work. and then I add this to the FCL as unit fpTimer ?

No problem, that's fine by me....

This would allow the LCL to implement a TTimerDriver descendent which integrates nicely in the LCL. The actually used driver is

Would you mind explaining this a bit more? What would the LCL do differently, "integrates nicely"?

Graeme Geldenhuys General error, hit any user to continue.
Michael Van Canneyt
to Graeme Geldenhuys
Graeme Geldenhuys wrote 21 lines
On 5/31/07, Michael Van Canneyt wrote: > > Do you mind if I split up the implementation like this: > > TTimer - Exposes all needed properties, events. Delegates work > to TTimerDriver. > TTimerDriver - Abstract class which has 2 methods and a property to > do the actual work. > TThreadedTimerDriver - TTimerDriver Descendent which uses your > implementation to do the actual work. > > and then I add this to the FCL as unit fpTimer ? No problem, that's fine by me.... > This would allow the LCL to implement a TTimerDriver descendent > which integrates nicely in the LCL. The actually used driver is Would you mind explaining this a bit more? What would the LCL do differently, "integrates nicely"?

Instead of using the default timer which creates a thread, it can create a TTimerDriver which asks e.g. MS-Windows for a timer event and handle everything through the event loop. Same for X11 (or however they do it there).

In other words, the LCL can use system timers if available, and integrate them with the normal event loop. I think this is preferable to threaded timers, which make applications heavier.

Also, you could improve the threaded timer with a pooled threaded timer (1 thread which does the work for all timers). If you use a lot of timers, that will be easier on resources.

Michael.