Board index » delphi » label.caption doesn't imediately execute

label.caption doesn't imediately execute

My Delphi 5 program changes a label on the form to indicate that data is
being processed.  After that instruction, THEN the process begins.  Yet the
label is not being updated until after the processing .....code snippet
below:

  form1.label1.caption:='beginning data analysis';
  load_files;
 etc.....do stuff here.....

I appreciate an explanation of why, and a workaround if possible.  Thanks!

 

Re:label.caption doesn't imediately execute


Quote
Mike wrote in message ...
>My Delphi 5 program changes a label on the form to indicate that data is
>being processed.  After that instruction, THEN the process begins.  Yet
the
>label is not being updated until after the processing .....code snippet
>below:

>  form1.label1.caption:='beginning data analysis';
>  load_files;
> etc.....do stuff here.....

>I appreciate an explanation of why, and a workaround if possible.  Thanks!

Windows is a message-based system. Changing a label's caption
is implemented by sending it a Windows message, with one of its
parameters being a pointer to the new text.

Sending messages can be done synchronously (SendMessage), which
actually calls the window procedure that would handle the message,
and has the message handled before the SendMessage call returns,
or asynchronously (PostMessage), which merely adds the message to
a message queue where eventually that same window procedure will
get around to handling it.

Setting the new caption is probably done synchronously. But that's
only half the story; after changing the text, it has to be drawn
on the screen. In Windows terminology, the control needs to paint
itself. This is probably the step that is only taken when your
processing is already over. The window procedure for that control
is running in the same thread as your number-crunching, and the
number-crunching part simply does not release control.

The workaround is quite easy if you know how these things work.
You can either call Application.ProcessMessages, which handles all
the Windows messages currently waiting in the queue, or call
Label.Update to have the label handle any outstanding repaint
requests.

If your processing takes any noticeable time at all, it is usually
a good idea to call ProcessMessages every so often. This ensures
that your program remains responsive to things that arrive by Windows
messages, like mouse movements, keyboard input, and repaint requests
(if you move another window across it, your program needs to repaint
itself afterwards).

Read up on WM_PAINT, Invalidate, Refresh, and Update.

Groetjes,
Maarten Wiltink

Re:label.caption doesn't imediately execute


on 30 mei 2003,    "Mike"  wrote  :

Quote
> My Delphi 5 program changes a label on the form to indicate that data
> is being processed.  After that instruction, THEN the process begins.
> Yet the label is not being updated until after the processing
> .....code snippet below:

>   form1.label1.caption:='beginning data analysis';
>   load_files;
>  etc.....do stuff here.....

> I appreciate an explanation of why, and a workaround if possible.
> Thanks!

load_files is apparently build as a tight loop, so Windows doesn't get a
chance to process it's message loop, and hence will not update the form.

Either intersperse the load_files proc. with Application.ProcessMessages,
or force the label to update :

form1.label1.caption:='beginning data analysis';
form1.label1.repaint;
load_files;

--
Regards - Dirk
http://users.pandora.be/dirk.claessens2
Attention: All spamshields raised; E-mails will bounce!
---------------------------------------------------------

Re:label.caption doesn't imediately execute


Quote
> The workaround is quite easy if you know how these things work.
> You can either call Application.ProcessMessages, which handles all
> the Windows messages currently waiting in the queue, or call
> Label.Update to have the label handle any outstanding repaint
> requests.

Make sure if you use Application.ProcessMessages, you take some action to
make sure that your computation can't be started more than once...

My favorite is one recommended by Allan LLoyd (IIRC, sorry if I'm wrong) on
one of these NGs about half a year ago...

say your event handler (that does the work) is called Button1Click and
responds to the OnClick event of Button1.

then in the first line of Button1Click (or at least somewhere before the
Application.ProcessMessages, it doesn't necessarily have to be the first
line) put:

Button1.OnClick := nil;

then after the processmessages (the last one you do in the event handler, or
the last procedure that calls ProcessMessages in your event handler), put

Button1.OnClick := Button1Click;

Kelly

Re:label.caption doesn't imediately execute


thanks for the help!  i'll read up on topics listed, and try suggested
presented.  I appreciate it!

Quote
"Dirk Claessens" <will.bounce@invalid> wrote in message

news:Xns938B9CC1FD59Fwillbounceinvalid@195.130.132.65...
Quote
> on 30 mei 2003,    "Mike"  wrote  :

> > My Delphi 5 program changes a label on the form to indicate that data
> > is being processed.  After that instruction, THEN the process begins.
> > Yet the label is not being updated until after the processing
> > .....code snippet below:

> >   form1.label1.caption:='beginning data analysis';
> >   load_files;
> >  etc.....do stuff here.....

> > I appreciate an explanation of why, and a workaround if possible.
> > Thanks!

> load_files is apparently build as a tight loop, so Windows doesn't get a
> chance to process it's message loop, and hence will not update the form.

> Either intersperse the load_files proc. with Application.ProcessMessages,
> or force the label to update :

> form1.label1.caption:='beginning data analysis';
> form1.label1.repaint;
> load_files;

> --
> Regards - Dirk
> http://users.pandora.be/dirk.claessens2
> Attention: All spamshields raised; E-mails will bounce!
> ---------------------------------------------------------

Re:label.caption doesn't imediately execute


Hi Mike.

As stated before you app continues the execution before windows gets a
chance to repaint. Simply put the following command after the change of the
label: Application.ProcessMessages.

The functionality of this is like the old Yield command, that allows windows
to handle other processes before continuing your application.

Quote
"Mike" <no...@hotmail.com> wrote in message

news:tnIBa.2578$Ly2.479924@cletus.bright.net...
Quote
> My Delphi 5 program changes a label on the form to indicate that data is
> being processed.  After that instruction, THEN the process begins.  Yet
the
> label is not being updated until after the processing .....code snippet
> below:

>   form1.label1.caption:='beginning data analysis';
>   load_files;
>  etc.....do stuff here.....

> I appreciate an explanation of why, and a workaround if possible.  Thanks!

Re:label.caption doesn't imediately execute


thanks all!  got it working just fine now!

"Plettner-Data - MerCator" <kenn...@plettner-data.dk> wrote in message
news:bb8fm4$2k8$1@news.{*word*104}city.dk...

Quote
> Hi Mike.

> As stated before you app continues the execution before windows gets a
> chance to repaint. Simply put the following command after the change of
the
> label: Application.ProcessMessages.

> The functionality of this is like the old Yield command, that allows
windows
> to handle other processes before continuing your application.

> "Mike" <no...@hotmail.com> wrote in message
> news:tnIBa.2578$Ly2.479924@cletus.bright.net...
> > My Delphi 5 program changes a label on the form to indicate that data is
> > being processed.  After that instruction, THEN the process begins.  Yet
> the
> > label is not being updated until after the processing .....code snippet
> > below:

> >   form1.label1.caption:='beginning data analysis';
> >   load_files;
> >  etc.....do stuff here.....

> > I appreciate an explanation of why, and a workaround if possible.
Thanks!

Re:label.caption doesn't imediately execute


Quote
Plettner-Data - MerCator wrote in message ...

[...]

Quote
>The functionality of this is like the old Yield command, that allows
windows
>to handle other processes before continuing your application.

"Yield" is now written "Sleep(0)". It's A Feature.

Groetjes,
Maarten Wiltink

Re:label.caption doesn't imediately execute


Quote
"Kelly Leahy" <kellyle...@swbell.net> wrote in message

news:e%JBa.1247$h25.696693810@newssvr30.news.prodigy.com...

Quote
> Make sure if you use Application.ProcessMessages, you take some action to
> make sure that your computation can't be started more than once...

> My favorite is one recommended by Allan LLoyd (IIRC, sorry if I'm wrong)
on
> one of these NGs about half a year ago...

> Button1.OnClick := nil;

> then after the processmessages (the last one you do in the event handler,
or
> the last procedure that calls ProcessMessages in your event handler), put

> Button1.OnClick := Button1Click;

Might have been me that suggested it, that's my favourite method. Also nice
for TTimers - stops the event from running again while it's still going (If
you use application.processmessages inside your timer)

Cheers,
Nicholas Sherlock

Other Threads