Board index » delphi » TTimer: Creating one at runtime and using onTimer event

TTimer: Creating one at runtime and using onTimer event

I have created a Unit without a form and I want to dynamically create a
timer and use it's onTimer event. I create a global variable called timer1
of type TTimer and then I created the timer Timer1 := TTimer.Create(Timer1);
//self did not work.
I was then able to set the interval and enabled properties.
All this works great but I also created a procedure Timer1Timer(Sendor:
TObject);
but this event is never executed?

How do I tie the dynamic timer to it onTimer event?

 

Re:TTimer: Creating one at runtime and using onTimer event


Quote
Jeff Howard wrote:
> I have created a Unit without a form and I want to dynamically create a
> timer and use it's onTimer event. I create a global variable called timer1
> of type TTimer and then I created the timer Timer1 := TTimer.Create(Timer1);
> //self did not work.
> I was then able to set the interval and enabled properties.
> All this works great but I also created a procedure Timer1Timer(Sendor:
> TObject);
> but this event is never executed?

Did you assign the handler?

 Timer1.OnTimer := Timer1Timer;
 Timer1.Enabled := True;

By the way, your handler can have any name and you can assign the same handler
to multiple controls.

 Timer1.OnTimer := Timer1Timer;
 Timer2.OnTimer := Timer1Timer;
 Timer3.OnTimer := Timer1Timer;

Re:TTimer: Creating one at runtime and using onTimer event


Quote
Jeff Howard wrote:
> I was able to make it work when I use this on TForm. Is this not possible
> within a unit without a form?

Try to add timer into DataModule.

Alex.

Other Threads