Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with unexpected behaviour 2

Status
Not open for further replies.

grion77

Programmer
Sep 4, 2006
3
IT
Hello. I'm just learning Delphi and I wrote this simple code to respond to the On Click Event of one Button.

I don't understand why while running the program first the for..loop is executed and only after this the Label1.Caption is updated. I was expecting the opposite behaviour (first label and then for..loop). Is this a problem related to the use of Sleep? I didn't modify any setting. I'm using Delphi 7.

Thanks for reading!

(this procedure simply displays a countdown checking every second a different itemof a radiogroup):

procedure TForm1.ButtonForClick(Sender: TObject);
var
I: Integer;

begin
Label1.Caption := 'For..Loop is running';
for I := (RadioGroup1.Items.Count) downto 0 do
begin
RadioGroup1.ItemIndex := I - 1;
Sleep(1000);
end;
end;
 
It probably is due to the Sleep call.

Try putting:
Code:
Application.ProcessMessages;
before your for loop line.

Clive
Runner_1Revised.gif

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer." (Paul Ehrlich)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
the answer is really simple, windows applications use a message loop for it's GUI and other things. the application never gets the chance to update the label because its busy with your for loop. So all you have to do is force the app to check it's message queue: call application.processmessages in long loops and display updating will take place.



-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
Application.ProcessMessages did the job.

Thanks!

I was really impressed by the speed and the quality of your suggestions!

Can you tell me where I can read more about

"windows applications use a message loop for it's GUI and other things"

in the Delphi help?
 
I doubt there's much by way of explanation in the Delphi help because whosrdaddy's comment is information regarding how the operating system operates. Check out these links for some background reading:

Clive
Runner_1Revised.gif

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer." (Paul Ehrlich)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top