tags:

views:

128

answers:

2

How could one reset the TTimer1 so even ontimer triggers, and the count restarted?

The Question is for Design Time command to act on Run-time.

+5  A: 

I suppose you actually mean that

var
  Timer1: TTimer;

Then, to reset the timer, do

Timer1.Enabled := false;
Timer1.Enabled := true;

In my "RejbrandCommon.pas" standard library, I have actually defined

procedure RestartTimer(Timer: TTimer);
begin
  Timer.Enabled := false;
  Timer.Enabled := true;
end;

Then, every time I need to restart I timer, I just do

RestartTimer(Timer1);

Of course, if you want the OnTimer procedure (e.g. Timer1Timer) to trigger prior to restart, you have to do

Timer1.OnTimer(Self);
Timer1.Enabled := false;
Timer1.Enabled := true;

or define

procedure TriggerAndRestartTimer(Timer: TTimer);
begin
  Timer.OnTimer(nil);    
  Timer.Enabled := false;
  Timer.Enabled := true;
end;

(Of course, the last procedure, TriggerAndRestartTimer, is not a method, and hence there is no Self. However, most likely the Timer1Timer procedure doesn't care about the Sender property, so you can send just anything, such as nil instead of Self.)

Andreas Rejbrand
tried this , not working.
none
I know it works. Do it all the time. Why doesn't it work? Isn't the timer reset, is the event not fireing, doesn't the code compile, does the computer explode?
Andreas Rejbrand
@none: This definitely works.
gabr
@Andreas yes, the event did not fired.
none
+1  A: 

There's no way to do it in design time. Have the handler disable the TTimer then re-enable it.

Ignacio Vazquez-Abrams