Board index » delphi » using TTimer outside of a form

using TTimer outside of a form

Hi,

I want to have something happen at a set elaped time, so TTimer seems
like a good choice.  However, I do not want to make it a child of a
form.  Is this allowed?  I constructed a varible of type TTimer, that's
fine.  But I run into problems when I assign its OnTimer event to a
procedure that I defined.  Delphi complained of incompatibility between
TNotifyEvent and procedure, although I wrote it as though the timer were
part of a form (except the name of the procedure does not have the name
of a form attached, of course).

Any help will be much appreciated!

Jeff

 

Re:using TTimer outside of a form


Quote
> I want to have something happen at a set elaped time, so TTimer seems
> like a good choice.  However, I do not want to make it a child of a
> form.  Is this allowed?  I constructed a varible of type TTimer, that's
> fine.  But I run into problems when I assign its OnTimer event to a
> procedure that I defined.  Delphi complained of incompatibility between
> TNotifyEvent and procedure, although I wrote it as though the timer were
> part of a form (except the name of the procedure does not have the name
> of a form attached, of course).

Jeff,

the timer event handler has to be a method of some object but the object
need not be a form. YOu can declare a simple TObject descendent like

 Type
   TTimerServer = Class
     FTimer: TTimer;
   public
     Constructor Create( aTimer: TTimer );
     Destructor Destroy; override;
     Procedure TimerOnTimer( Sender: TObject );
   End;

 Procedure TTimerServer.TimerOnTimer( sender: TObject );
 Begin
   your code here
 End;  

 Constructor TTimerServer.Create( aTimer: TTimer );
 Begin
   inherited Create;
   FTimer := aTimer;
   FTimer.onTimer := TimerOnTimer;
 End;

 Destructor TTimerServer.Destroy;
 Begin
   If Assigned( FTimer ) Then
     FTimer.OnTimer := Nil;
   inherited Destroy;
 End;  

 Create an instance of this class to serve your TTimer instance.

Peter Below (TeamB)  100113.1...@compuserve.com)
No e-mail responses, please, unless explicitely requested!

Other Threads