Recent

Author Topic: [CLOSED] TTimer accuracy  (Read 1471 times)

pcurtis

  • Hero Member
  • *****
  • Posts: 634
[CLOSED] TTimer accuracy
« on: May 08, 2021, 04:33:30 pm »
Consider this code

Code: Pascal  [Select][+][-]
  1. { TForm1 }
  2.  
  3. procedure TForm1.FormCreate(Sender: TObject);
  4. begin
  5.   Timer1.Enabled := false;
  6.   Timer2.Enabled := false;
  7.   Timer1.Interval := 999;
  8.   Timer2.Interval := 1000;
  9.   Timer1.Enabled := true;
  10.   Timer2.Enabled := true;
  11. end;
  12.  
  13. procedure TForm1.Timer1Timer(Sender: TObject);
  14. begin
  15.   Timer2.Enabled := false;
  16.   Timer2.Enabled := true;
  17. end;
  18.  
  19. procedure TForm1.Timer2Timer(Sender: TObject);
  20. begin
  21.   Beep(1000, 200);
  22. end;
  23.  

In theory I should never hear a beep, but I do.

If I change timer1.interval := 980 all is OK.

Is this to be expected?
« Last Edit: May 08, 2021, 05:22:53 pm by pcurtis »
Windows 10
Laz 2.0.10
FPC 3.2.0

MarkMLl

  • Hero Member
  • *****
  • Posts: 2844
Re: TTimer accuracy
« Reply #1 on: May 08, 2021, 05:01:16 pm »
Yes.
Turbo Pascal v1 on CCP/M-86, multitasking with LAN and graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Handoko

  • Hero Member
  • *****
  • Posts: 4314
  • My goal: build my own game engine using Lazarus
Re: TTimer accuracy
« Reply #2 on: May 08, 2021, 05:07:41 pm »
I haven't tried but maybe OP needs a high precision timer.

Try these:
https://forum.lazarus.freepascal.org/index.php/topic,51067.msg374383.html#msg374383
https://forum.lazarus.freepascal.org/index.php/topic,52475.msg386792.html#msg386792

My advice, don't use/run more than 1 timer at the same time. You may get hard to debug issue if you're a beginner.

pcurtis

  • Hero Member
  • *****
  • Posts: 634
Re: TTimer accuracy
« Reply #3 on: May 08, 2021, 05:21:53 pm »
OK thanks
Windows 10
Laz 2.0.10
FPC 3.2.0

Handoko

  • Hero Member
  • *****
  • Posts: 4314
  • My goal: build my own game engine using Lazarus
Re: [CLOSED] TTimer accuracy
« Reply #4 on: May 08, 2021, 06:01:04 pm »
There are the solutions for using 1 TTimer to do 2 tasks on 1500ms and 2000ms:

Code: Pascal  [Select][+][-]
  1. // Solution 1
  2.  
  3. procedure TForm1.FormCreate(Sender: TObject);
  4. begin
  5.   Timer1.Interval := 500;
  6.   Timer1.Enabled  := True;
  7. end;
  8.  
  9. procedure TForm1.Timer1Timer(Sender: TObject);
  10. const
  11.   Passes: Integer = 0;
  12. begin
  13.   Inc(Passes);
  14.   case Passes of
  15.     3: ShowMessage('Doing first task.');
  16.     4: begin
  17.          Timer1.Enabled := False;
  18.          ShowMessage('Doing second task.');
  19.        end;
  20.   end;
  21. end;

Code: Pascal  [Select][+][-]
  1. // Solution 2
  2.  
  3. procedure TForm1.FormCreate(Sender: TObject);
  4. begin
  5.   Timer1.Interval := 1500;
  6.   Timer1.Enabled  := True;
  7. end;
  8.  
  9. procedure TForm1.Timer1Timer(Sender: TObject);
  10. const
  11.   FirstTaskFinished: Boolean = False;
  12. begin
  13.   case FirstTaskFinished of
  14.     False:
  15.       begin
  16.         FirstTaskFinished := True;
  17.         Timer1.Interval   := 500;
  18.         ShowMessage('Doing first task.');
  19.       end;
  20.     True:
  21.       begin
  22.         Timer1.Enabled := False;
  23.         ShowMessage('Doing second task.');
  24.       end;
  25.   end;
  26. end;

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 955
    • Lebeau Software
Re: [CLOSED] TTimer accuracy
« Reply #5 on: May 08, 2021, 09:51:26 pm »
In theory I should never hear a beep, but I do.

Most system timers do not have 1ms precision like that.  For instance, on Windows, the system clock has ~15ms precision, IIRC.

Also, even with a 1ms precision, when the 1st timer has elapsed and generated its notification, by the time that notification is actually processed by your code and stops the 2nd timer, the 2nd timer may have already elapsed and generated its notification, which will still get processed.  Stopping a timer does not kill off notifications that have already been generated.

Is this to be expected?

Yes.
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

 

TinyPortal © 2005-2018