Recent

Author Topic: [Solved] Stopping TTimer after a fixed time.  (Read 1133 times)

HappyLarry

  • Full Member
  • ***
  • Posts: 155
[Solved] Stopping TTimer after a fixed time.
« on: October 21, 2020, 07:12:28 pm »
Is there a way of automatically stopping a timer after a certain time without using a button click or another timer?

I assume I can't use
Code: Pascal  [Select][+][-]
  1. Timer1.enabled := false

from inside the Timer1.Timer procedure.because then the Timer1.Timer procedure can't complete since the timer will have been disabled.
« Last Edit: October 21, 2020, 08:22:39 pm by HappyLarry »
Use Lazarus and Free Pascal and stand on the shoulders of giants . . . very generous giants. Thank you.

Handoko

  • Hero Member
  • *****
  • Posts: 5130
  • My goal: build my own game engine using Lazarus
Re: Stopping TTimer after a fixed time.
« Reply #1 on: October 21, 2020, 07:19:29 pm »
You can add a counter inside the timer. Reset the counter when the timer starts then increases the counter in the OnTimer event. If the counter reaches a certain value then disables the timer.

HappyLarry

  • Full Member
  • ***
  • Posts: 155
Re: Stopping TTimer after a fixed time.
« Reply #2 on: October 21, 2020, 07:27:42 pm »
I can write code like below but ....

Code: Pascal  [Select][+][-]
  1. If counter = 5000 then
  2. begin
  3.    timer1.enabled := false;
  4. end;
  5.  

which procedure does it go in? It can't go in Timer1.Timer because a timer can't disable itself.
Use Lazarus and Free Pascal and stand on the shoulders of giants . . . very generous giants. Thank you.

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: Stopping TTimer after a fixed time.
« Reply #3 on: October 21, 2020, 07:38:38 pm »
It can't go in Timer1.Timer because a timer can't disable itself.

Hi

Why???????

There is a lot of code around for time critical code which does this:

procedure TForm1.Timer1Timer(Sender: TObject);
begin
Timer1.enabled := false;
/// do time critical stuff
Timer1.enabled := true;
end;


Winni


winni

  • Hero Member
  • *****
  • Posts: 3197
Re: Stopping TTimer after a fixed time.
« Reply #4 on: October 21, 2020, 07:39:07 pm »
It can't go in Timer1.Timer because a timer can't disable itself.

Hi

Why???????

There is a lot of code around for time critical code which does this:
Code: Pascal  [Select][+][-]
  1.  
  2. procedure TForm1.Timer1Timer(Sender: TObject);
  3. begin
  4. Timer1.enabled := false;
  5. /// do time critical stuff
  6. Timer1.enabled := true;
  7. end;
  8.  

Winni

JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4459
  • I like bugs.
Re: Stopping TTimer after a fixed time.
« Reply #5 on: October 21, 2020, 07:40:03 pm »
Is there a way of automatically stopping a timer after a certain time without using a button click or another timer?

I assume I can't use
Code: Pascal  [Select][+][-]
  1. Timer1.enabled := false

from inside the Timer1.Timer procedure.because then the Timer1.Timer procedure can't complete since the timer will have been disabled.
Disabled means stopped. You wanted to stop it yourself. :)
The OnTimer event is a good place for it as Handoko wrote.
Mostly Lazarus trunk and FPC 3.2 on Manjaro Linux 64-bit.

Handoko

  • Hero Member
  • *****
  • Posts: 5130
  • My goal: build my own game engine using Lazarus
Re: Stopping TTimer after a fixed time.
« Reply #6 on: October 21, 2020, 07:47:48 pm »
... a timer can't disable itself.

Really? Are you sure?

HappyLarry

  • Full Member
  • ***
  • Posts: 155
Re: Stopping TTimer after a fixed time.
« Reply #7 on: October 21, 2020, 07:54:12 pm »
I had assumed that it was a bad idea to interrupt procedures in the middle of their execution before they have reached the final 'end; - i.e. have not completed. It is equivalent to using a 'Go To' statement (produces spaghetti code?).
« Last Edit: October 21, 2020, 07:58:19 pm by HappyLarry »
Use Lazarus and Free Pascal and stand on the shoulders of giants . . . very generous giants. Thank you.

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1312
    • Lebeau Software
Re: Stopping TTimer after a fixed time.
« Reply #8 on: October 21, 2020, 08:00:01 pm »
Is there a way of automatically stopping a timer after a certain time without using a button click or another timer?

Like others said, you can use a simple counter inside the timer.  Or, to account for time drifts (since TTimer is not a real-time timer), I would get the current time/ticks when starting the timer and then inside the timer check if the desired end time/ticks has passed.

I assume I can't use
Code: Pascal  [Select][+][-]
  1. Timer1.enabled := false

from inside the Timer1.Timer procedure.

You assume incorrectly.  You most certainly CAN do that.

because then the Timer1.Timer procedure can't complete since the timer will have been disabled.

The timer has ALREADY elapsed and your procedure is ALREADY running.  Disabling the timer from inside your procedure won't change that.  All it will do is stop the timer from firing any further events.

I had assumed that it was a bad idea to interrupt procedures in the middle of their execution before they have reached the final 'end; - i.e. have not completed (produces spaghetti code?).

You are not interrupting anything.  You are simply telling the timer to remove itself from being scheduled by the OS for any further OnTimer events.  Your procedure itself is still running and will continue running its code to its end naturally, eg:

Code: [Select]
procedure TForm1.Timer1Timer(Sender: TObject);
begin
  // code here runs normally...

  // code here runs normally...

  Timer1.Enabled := false;

  // code here runs normally...

  // code here runs normally...

  // code here runs normally...
end;
« Last Edit: October 21, 2020, 08:05:12 pm by Remy Lebeau »
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

HappyLarry

  • Full Member
  • ***
  • Posts: 155
Re: Stopping TTimer after a fixed time.
« Reply #9 on: October 21, 2020, 08:16:45 pm »
OK everyone, thank you.

I had assumed that the moment the timer si disabled, it ceases to execute any more code in its associated procedures but that is wrong - the timer completes the 'timer' procedure, despite being disabled.

 
Use Lazarus and Free Pascal and stand on the shoulders of giants . . . very generous giants. Thank you.

cdbc

  • Hero Member
  • *****
  • Posts: 1025
    • http://www.cdbc.dk
Re: [Solved] Stopping TTimer after a fixed time.
« Reply #10 on: October 22, 2020, 09:04:09 pm »
Hi
You're just setting a boolean value, that the timer checks every time round, so to speak :-)
.... Have a look at "TTHread.Terminate", Smells somewhat like what's going in a Thread-Loop .... Always checking Terminated ~ a boolean value ;-)
HTH - Regards Benny
« Last Edit: October 22, 2020, 09:06:06 pm by cdbc »
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

 

TinyPortal © 2005-2018