gave every label its own TTimer so they can think independently

Go down

gave every label its own TTimer so they can think independently

· #1

my form has 60 labels and i want each one to update on its own

so i put 60 TTimers on the form, one per label, all with Interval := 1

now the program uses a whole cpu core and the labels all change at the same time anyway. are they talking to each other

Re: gave every label its own TTimer so they can think independently

· #2

They are not talking to each other. They are waiting in line.

A TTimer does not run on its own. Every timer's OnTimer runs in the main thread, one after another, when the application gets around to it. Sixty timers do not give you sixty thinkers. They give you one thinker with sixty alarm clocks.

And Interval := 1 asks for a thousand alarms a second per timer. You will not get that many, but the program will try, which is where your CPU core went.

One timer does the same job:

pascal
procedure TForm1.Timer1Timer(Sender: TObject);
var
  I: Integer;
begin
  for I := 0 to ComponentCount - 1 do
    if Components[I] is TLabel then
      TLabel(Components[I]).Caption := TimeToStr(Now);
end;

Set its Interval to something a person can perceive, like 500.

Re: gave every label its own TTimer so they can think independently

· #3

ok but then the labels are not independent. they share a timer. it feels crowded

Re: gave every label its own TTimer so they can think independently

· #4

I shared one timer with an entire accounting package for eleven years. Nobody's feelings were hurt. The labels will cope.

Re: gave every label its own TTimer so they can think independently

· #5

gIvE eAcH lAbEl A tHrEaD

Re: gave every label its own TTimer so they can think independently

· #6

Do not give each label a thread.

demo_wave, that is your second warning this week, and the first one was for spelling.

Forum rules 1 to 10 apply. Yes, to you.

Re: gave every label its own TTimer so they can think independently

· #7

one timer now. cpu is at 0 percent. the labels seem fine

i deleted the other 59 timers to save space. also deleted Timer1 by accident. put it back

Go up

Sign in to reply