Recent

Author Topic: [Solved] How to use TTimer without a form  (Read 375 times)

Wilko500

  • Full Member
  • ***
  • Posts: 140
[Solved] How to use TTimer without a form
« on: June 10, 2025, 08:40:02 pm »
MacOs Laz 4.1 FPC 3.2.2
My program under development has six timers (TTimer) all set up on the main form. Currently they are coded so that each has both a fire and tick procedure.  They work just fine but I would like to put all these timers into a single unit eg TimerUnit.  I have been unable to get this to work so I went back to a simple example I found on the web, helped by AI lol, but it does not compile
Code: Pascal  [Select][+][-]
  1. unit TimerUnit;
  2.  
  3. interface
  4.  
  5. uses
  6.   SysUtils, Classes, extCrtls;
  7.  
  8. type
  9.   TMyTimer = class(TObject)
  10.   private
  11.     FTimer: TTimer;
  12.     FOnTimer: TTimerEvent;      // <=Compile error identifier not found TTimerEvent
  13.     procedure OnTimerEvent(Sender: TObject);
  14.   public
  15.     constructor Create;
  16.     destructor Destroy; override;
  17.     procedure Start(Interval: Integer; OnTimer: TTimerEvent);
  18.     procedure Stop;
  19.   end;
  20.  
  21. implementation
  22.  
  23. { TMyTimer }
  24.  
  25. constructor TMyTimer.Create;
  26. begin
  27.   FTimer := TTimer.Create(nil);
  28.   FTimer.Enabled := False;
  29.   FTimer.OnTimer := OnTimerEvent;
  30. end;
  31.  
  32. destructor TMyTimer.Destroy;
  33. begin
  34.   FTimer.Free;
  35.   inherited;
  36. end;
  37.  
  38. procedure TMyTimer.Start(Interval: Integer; OnTimer: TTimerEvent);
  39. begin
  40.   FTimer.Interval := Interval;
  41.   FOnTimer := OnTimer;
  42.   FTimer.Enabled := True;
  43. end;
  44.  
  45. procedure TMyTimer.Stop;
  46. begin
  47.   FTimer.Enabled := False;
  48. end;
  49.  
  50. procedure TMyTimer.OnTimerEvent(Sender: TObject);
  51. begin
  52.   FOnTimer(Sender);
  53. end;
  54.  
  55. end.
Compiler errors on line 12 with identifier not found TTimerEvent

Getting this working was supposed to be a starting point for migrating my existing code. Maybe one of you has a simple working example of a TTimer in a unit rather than a form?


« Last Edit: June 10, 2025, 10:36:56 pm by Wilko500 »
MacBook Pro mid 2015 with OS Monterey 12.7.6
FPC 3.2.3 Lazarus 3.7
FPC 3.2.2 Lazarus 3.4

Phoenix

  • Full Member
  • ***
  • Posts: 112
Re: How to use TTimer without a form
« Reply #1 on: June 10, 2025, 09:36:00 pm »
Code: Pascal  [Select][+][-]
  1. unit TimerUnit;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   SysUtils, Classes, ExtCtrls;
  9.  
  10. type
  11.   TMyTimer = class(TObject)
  12.   private
  13.     FTimer: TTimer;
  14.     FOnTimer: TNotifyEvent;
  15.     procedure OnTimerEvent(Sender: TObject);
  16.   public
  17.     constructor Create;
  18.     destructor Destroy; override;
  19.     procedure Start(Interval: Integer; OnTimer: TNotifyEvent);
  20.     procedure Stop;
  21.   end;
  22.  
  23. implementation
  24.  
  25. { TMyTimer }
  26.  
  27. constructor TMyTimer.Create;
  28. begin
  29.   FTimer := TTimer.Create(nil);
  30.   FTimer.Enabled := False;
  31.   FTimer.OnTimer := @OnTimerEvent;
  32. end;
  33.  
  34. destructor TMyTimer.Destroy;
  35. begin
  36.   FTimer.Free;
  37.   inherited;
  38. end;
  39.  
  40. procedure TMyTimer.Start(Interval: Integer; OnTimer: TNotifyEvent);
  41. begin
  42.   FTimer.Interval := Interval;
  43.   FOnTimer := OnTimer;
  44.   FTimer.Enabled := True;
  45. end;
  46.  
  47. procedure TMyTimer.Stop;
  48. begin
  49.   FTimer.Enabled := False;
  50. end;
  51.  
  52. procedure TMyTimer.OnTimerEvent(Sender: TObject);
  53. begin
  54.   FOnTimer(Sender);
  55. end;
  56.  
  57. end.
  58.  
  59.  

Wilko500

  • Full Member
  • ***
  • Posts: 140
Re: How to use TTimer without a form
« Reply #2 on: June 10, 2025, 10:36:40 pm »
@Phoenix, thank you.  Very helpful.  I see that AI inspired help has a little way to go but to be fair it was close.  Shame I wasn't sharp enough to spot the errors.  For now I'll mark this post as solved and then I'll see if I can get my more complicated timer procedure to work.

Update:  Took a few attempts but I now have the basic timer working to update main form time field with the timer code in the TimerUnit.  When I have more time I'll try with the more complicated timer code.  This should simplify my code because I think I can create a separate class instance for each of my timers and then have a separate Tick and Fire for each.  At the moment the entire timer code is duplicated for each timer. 
« Last Edit: June 11, 2025, 12:28:42 am by Wilko500 »
MacBook Pro mid 2015 with OS Monterey 12.7.6
FPC 3.2.3 Lazarus 3.7
FPC 3.2.2 Lazarus 3.4

 

TinyPortal © 2005-2018