Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2016-08

controling a loop

11 messages · 9 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Schindler Karl-Michael

Hi.

I want to control the execution of a loop with a checkbox. If the checkbox is checked the loop should run, if it is unchecked no action. I tried this with onClick:

CheckBox.Click(Sender:TObject);
begin
  repeat
    Some Action …
    Form.Update;
  until (CheckBox.State  checked)
end;

The loop starts, but i cannot click the Checkbox again for switching off. How to do it? Is it possible without threads?

Michael.

Reply by email

Maxi
to Schindler Karl-Michael
CheckBox.Click(Sender:TObject);
begin
  repeat
    Some Action …
    Appication.ProcessMessage;
  until (CheckBox.State  checked)
end;

2016-08-10 10:56 GMT-03:00 Schindler Karl-Michael <

Show quoted text 26 lines
: Hi. I want to control the execution of a loop with a checkbox. If the checkbox is checked the loop should run, if it is unchecked no action. I tried this with onClick: CheckBox.Click(Sender:TObject); begin repeat Some Action … Form.Update; until (CheckBox.State checked) end; The loop starts, but i cannot click the Checkbox again for switching off. How to do it? Is it possible without threads? Michael. -- _______________________________________________ Lazarus mailing list http://lists.lazarus-ide.org/listinfo/lazarus
El que pregunta aprende, y el que contesta aprende a responder. No a la obsolecencia programada: http://www.rtve.es/noticias/20110104/productos-consumo-duran-cada-vez-menos/392498.shtml Linux User #495070 http://domonetic.com/blog -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.lazarus-ide.org/pipermail/lazarus/attachments/20160810/5a0a9840/attachment.html>

Reply by email

Dmitry Boyarintsev
to Schindler Karl-Michael: Hi. I want to control the execution of a loop with a checkbox. If the checkbox is checked…

On Wed, Aug 10, 2016 at 9:56 AM, Schindler Karl-Michael <

Show quoted text 6 lines
wrote: The loop starts, but i cannot click the Checkbox again for switching off. How to do it? Is it possible without threads? If SomeAction blocks a thread, then you should be using thread.

thanks,
Dmitry
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus-ide.org/pipermail/lazarus/attachments/20160810/5d45cc44/attachment.html>

Reply by email

Schindler Karl-Michael
Maxi wrote 18 lines
Date: Wed, 10 Aug 2016 11:01:54 -0300 From: Maxi To: Lazarus mailing list Subject: Re: [Lazarus] controling a loop Message-ID: Content-Type: text/plain; charset="utf-8" CheckBox.Click(Sender:TObject); begin repeat Some Action … Appication.ProcessMessage; until (CheckBox.State checked) end;

Besides the typo ;-) Application.ProcessMessages did the trick. Thanks.

Reply by email

JuuS
to Schindler Karl-Michael

Hi,

Besides the typo ;-) Application.ProcessMessages did the trick. Thanks.

Don't know if this is an issue anymore, it was in Delphi some time ago so you may want to check:

It used to be Application.processmessages pegged cpu to 100%. The alternative "application.handlemessage" may be a bit slower but much nicer on the cpu. Again, don't know if this is an issue in Lazarus.

Reply by email

Paul Breneman
to JuuS
JuuS wrote 13 lines
Hi, > > Besides the typo ;-) Application.ProcessMessages did the trick. Thanks. > Don't know if this is an issue anymore, it was in Delphi some time ago so you may want to check: It used to be Application.processmessages pegged cpu to 100%. The alternative "application.handlemessage" may be a bit slower but much nicer on the cpu. Again, don't know if this is an issue in Lazarus.

A loop with Application.ProcessMessages with 100% CPU usage will also use up your battery on a laptop or mobile device. An easy way to improve that is to put sleep(10) into the loop.

Here a short explanation of the alternative I use most of the time: http://codenewsfast.com/cnf/article/0/permalink.art-ng1824q786

You can find the above link and other things here: http://turbocontrol.com/APro.htm

Regards,
Paul
www.ControlPascal.com

Reply by email

Mattias Gaertner
to JuuS: Don't know if this is an issue anymore, it was in Delphi some time ago so you may want to…
Show quoted text 12 lines
Hi, > > Besides the typo ;-) Application.ProcessMessages did the trick. Thanks. > Don't know if this is an issue anymore, it was in Delphi some time ago so you may want to check: It used to be Application.processmessages pegged cpu to 100%. The alternative "application.handlemessage" may be a bit slower but much nicer on the cpu. Again, don't know if this is an issue in Lazarus.

HandleMessages calls ProcessMessages and then sleeps til a new message. This is Delphi compatible and by design.

100% cpu without need is bad coding.

Mattias

Reply by email

Michael Schnell
to Schindler Karl-Michael: Hi. I want to control the execution of a loop with a checkbox. If the checkbox is checked…
Schindler Karl-Michael wrote
I want to control the execution of a loop with a checkbox. If the checkbox is checked the loop should run, if it is unchecked no action. I tried this with onClick:

Something like that should never be done in an "Event driven" system like a Lazarus application.

Each event should be finished (ad the handler procedure exited) as fast as possible.

So you should exit the procedure when the check box is not checked and start a new execution in the "CheckBox1Change" event.

-Michael

Reply by email

Bart
to Michael Schnell
Michael Schnell wrote
Each event should be finished (ad the handler procedure exited) as fast as possible.

If you extend this (into ludicracy) this would mean that exch event should fire a new thread that rubs the code needed and then exit... ;-)

Bart

Reply by email

Lukasz Sokol
to Bart
Bart wrote 10 lines
On 8/12/16, Michael Schnell wrote: > Each event should be finished (ad the handler procedure exited) as fast > as possible. If you extend this (into ludicracy) this would mean that exch event should fire a new thread that rubs the code needed and then exit... ;-)

Well probably if the event is required to do something useful but time-consuming (like launching a long long calculation) while the GUI (thread) needs to be responsive (e.g. report on progress, maybe abort the calculation or change parameters in-flight)

Bart

el es

Reply by email

Michael Schnell
to Bart: If you extend this (into ludicracy) this would mean that exch event should fire a new…
Bart wrote
If you extend this (into ludicracy) this would mean that exch event should fire a new thread that rubs the code needed and then exit... ;-)

Depending on the exact task in question this indeed might be perfectly viable.

But doing a busy loop (resulting in 100% do-nothing CPU) on behalf of a GUI switch IMHO is not really appropriate ever.

-Michael

Reply by email