Is there a way to pause the TTimer?

I couldn't find a solution on adding a pause method on TTimer. Please help me.
edeauxAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

Wayne BarronAuthor, Web DeveloperCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.