Is there a way to pause the TTimer?

Posted on 2004-11-19
Last Modified: 2010-04-04
I couldn't find a solution on adding a pause method on TTimer. Please help me.
Question by:edeaux
    LVL 14

    Expert Comment

    Timer1.Enabled := False;
    LVL 6

    Expert Comment

    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.

    Expert Comment

    Perhaps you can add a boolean var, set it to true when you want and use something like this in OnTimer:

    if BoolVar then

    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.
    LVL 30

    Expert Comment

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

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


    Author Comment

    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.

    Expert Comment

    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;
     if not AudioPlaying then
       do stuff...
    LVL 33

    Accepted Solution

    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 declarations }
        TimerTime, cInterval: Cardinal;

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

    procedure TForm1.sbut_StartTimerClick(Sender: TObject);
    // 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;

    procedure TForm1.sbut_PauseTimerClick(Sender: TObject);
    // 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
      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
      if cInterval = 0 then Exit;
      Timer1.Interval := cInterval;
      // set new interval
      TimerTime := GetTickCount; // get new TimerTime
      Timer1.Enabled := True;
      sbut_PauseTimer.Caption := 'Pause Timer';

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

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

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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Suggested Solutions

    Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
    Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now