<div dir="ltr"><div>The problem of your code is that the variable <answer> is not initialized. <br></div><div>Regards</div><div>Evgueny<br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">вт, 27 окт. 2020 г. в 15:28, Santiago A. via lazarus <<a href="mailto:lazarus@lists.lazarus-ide.org">lazarus@lists.lazarus-ide.org</a>>:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<div>
<div>El 18/10/2020 a las 19:18, Lars via
lazarus escribió:<br>
</div>
<blockquote type="cite">When building
a simple TTimer demo I cannot seem to get it working
<br>
<br>
Any idea what the problem could be if you paste this code into
your form with a memo?
<br>
<br>
var
<br>
TimeSpent: integer;
<br>
<br>
procedure TForm2.Button1Click(Sender: TObject);
<br>
var
<br>
i, answer: integer;
<br>
begin
<br>
Timer1.enabled := false;
<br>
TimeSpent := 0;
<br>
Timer1.Enabled := true;
<br>
Timer1.interval := 1;
<br>
for i := 0 to 999999999 do
<br>
begin
<br>
answer := i * answer;
<br>
end;
<br>
<br>
memo1.lines.add('time spent: ' + inttostr(timespent));
<br>
<br>
end;
<br>
<br>
procedure TForm2.Timer1Timer(Sender: TObject);
<br>
begin
<br>
inc(TimeSpent);
<br>
end;
<br>
<br>
It says
<br>
time spent: 0
<br>
Whereas the time should be a lot.
<br>
<br>
Regards,
<br>
Lars
<br>
</blockquote>
<br>
I don't know what are you trying to do, but if you are trying to
find out how long it takes certain process, you should try other
approach. Timer is low precision and it is only fired by events, so
you must process event's queue.<br>
<br>
A first and bad approach:<br>
<pre>for i := 0 to 999999999 do
begin
answer := i * answer;
application.processmessages; //<-- process event queue
end;
</pre>
But this is very not a very efficient way. The best is to get the
start time, get the end time and subtract.<br>
<pre>var
StartTime,EndTime:TDataTime;
i, answer: integer;
begin
StartTime:=now;
for i := 0 to 999999999 do
begin
answer := i * answer;
end;
EndTime:=now;
memo1.lines.add('time spent: ' + TimeToStr(EndTime-StarTime) );
end;</pre>
But TDateTime is not accurate at all if you are measuring short
periods (milliseconds). <br>
<br>
EpikTimer is a component with much better precision.<br>
<br>
<a href="https://wiki.lazarus.freepascal.org/EpikTimer" target="_blank">https://wiki.lazarus.freepascal.org/EpikTimer</a><br>
<br>
<br>
<pre cols="72">--
Saludos
Santiago A.</pre>
</div>
-- <br>
_______________________________________________<br>
lazarus mailing list<br>
<a href="mailto:lazarus@lists.lazarus-ide.org" target="_blank">lazarus@lists.lazarus-ide.org</a><br>
<a href="https://lists.lazarus-ide.org/listinfo/lazarus" rel="noreferrer" target="_blank">https://lists.lazarus-ide.org/listinfo/lazarus</a><br>
</blockquote></div>