• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 629
  • Last Modified:

Is there a way to pause the TTimer?

I couldn't find a solution on adding a pause method on TTimer. Please help me.
0
edeaux
Asked:
edeaux
1 Solution
 
DragonSlayerCommented:
Timer1.Enabled := False;
0
 
Imthiyaz_phCommented:
A timer cannot be paused and resumed. Setting the enabled property to False kills the windows timer and setting it to True recreates the windows timer.

The best solution is to create a thread and implement the timer's code into the thread.
A thread can be suspended and its execution can be resumed from where it left off.
0
 
BindzaCommented:
Perhaps you can add a boolean var, set it to true when you want and use something like this in OnTimer:

if BoolVar then
 Sleep(pause_time)

But you should use the Enabled property. If you do something in the OnTimer event - just use temporary store variables, and when it resets assign their values to the stuff that gets calculated.

There's not much any of us can do it you don't describe what you wanna do a little better.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Wayne BarronCommented:
YOu can try something like this, It is what I use.

procedure TForm1.Button1Click(Sender: TObject);
const
  Captions: array[Boolean] of PChar = ('Watch Go','Stop Watch');
begin
  MyTimer.Enabled := not MyTimer.Enabled;
  Button1.Caption := Captions[MyTimer.Enabled];
end;

Carrzkiss
0
 
edeauxAuthor Commented:
i'm making a program that ables to play audio and has a timer. everytime the audio is played the timer  must be paused and will resume if the audio is done playing. could someone help me this? i just want the timer to have a pause and resume method.
0
 
BindzaCommented:
Well than just disable the timer when the audio starts and enable it when it's finished. Or add a bool var like this:

AudioPlaying: boolean;

procedure Timer1.OnTimer;
begin
 if not AudioPlaying then
  begin
   do stuff...
  end;
end;
0
 
Slick812Commented:
hello edeaux , I can not see your reason for this question, as has been said here already, the  TTimer is not meant to pause, it is a system timer and is either enabled (running) or disabled (Stoped), but if you want to "Pause" it, then just do some math and subtract the current  TickCount  from the the  starting   TickCount  at the pause moment, then restart the timer for unpause with a new interval value of the old Timer Interval minus the elapsed tickcount ,  ,
some code for that



  private
    { Private declarations }
    TimerTime, cInterval: Cardinal;



procedure TForm1.FormCreate(Sender: TObject);
begin
cInterval := 0;
TimerTime := 0;
// set time recorders to zero
end;


procedure TForm1.sbut_StartTimerClick(Sender: TObject);
begin
// this is a button click that will start the timer, you need to record the interval and tick count

Timer1.Interval := 10000;
// record the curent interval as the Timer1.Interval
cInterval := Timer1.Interval;
// record the timer start up in TimerTime
TimerTime := GetTickCount;
Timer1.Enabled := True;
end;

procedure TForm1.sbut_PauseTimerClick(Sender: TObject);
begin
// this is a button click that will pause and restart the timer
// you need to record the elasped time and adjust the current interval
if Timer1.Enabled then
  begin
  if TimerTime = 0 then Exit;
  TimerTime := GetTickCount - TimerTime;
  // get the elapsed ticks in the TimerTime by subtracting
  cInterval := Timer1.Interval - TimerTime;
  // subtrack the elapsed time from current interval
  Timer1.Enabled := False;
  sbut_PauseTimer.Caption := 'UnPause Timer';
  end else
  begin
  if cInterval = 0 then Exit;
  Timer1.Interval := cInterval;
  // set new interval
  TimerTime := GetTickCount; // get new TimerTime
  Timer1.Enabled := True;
  sbut_PauseTimer.Caption := 'Pause Timer';
  end;
end;


procedure TForm1.Timer1Timer(Sender: TObject);
begin
Timer1.Enabled := False;
TimerTime := 0; // reset recorders to zero
cInterval := 0;
ShowMessage('Timer1 Event');
end;


 = = = = = = = = = = =  == = = = = = = =  = = =

this seems to work for me
ask questions if you need more
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now