Forum > LCL

[CLOSED] TTimer accuracy

(1/2) > >>

pcurtis:
Consider this code


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---{ TForm1 } procedure TForm1.FormCreate(Sender: TObject);begin  Timer1.Enabled := false;  Timer2.Enabled := false;  Timer1.Interval := 999;  Timer2.Interval := 1000;  Timer1.Enabled := true;  Timer2.Enabled := true;end; procedure TForm1.Timer1Timer(Sender: TObject);begin  Timer2.Enabled := false;  Timer2.Enabled := true;end; procedure TForm1.Timer2Timer(Sender: TObject);begin  Beep(1000, 200);end;  
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?

MarkMLl:
Yes.

Handoko:
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:
OK thanks

Handoko:
There are the solutions for using 1 TTimer to do 2 tasks on 1500ms and 2000ms:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---// Solution 1 procedure TForm1.FormCreate(Sender: TObject);begin  Timer1.Interval := 500;  Timer1.Enabled  := True;end; procedure TForm1.Timer1Timer(Sender: TObject);const  Passes: Integer = 0;begin  Inc(Passes);  case Passes of    3: ShowMessage('Doing first task.');    4: begin         Timer1.Enabled := False;         ShowMessage('Doing second task.');       end;  end;end;

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---// Solution 2 procedure TForm1.FormCreate(Sender: TObject);begin  Timer1.Interval := 1500;  Timer1.Enabled  := True;end; procedure TForm1.Timer1Timer(Sender: TObject);const  FirstTaskFinished: Boolean = False;begin  case FirstTaskFinished of    False:      begin        FirstTaskFinished := True;        Timer1.Interval   := 500;        ShowMessage('Doing first task.');      end;    True:      begin        Timer1.Enabled := False;        ShowMessage('Doing second task.');      end;  end;end;

Navigation

[0] Message Index

[#] Next page

Go to full version