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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Unusual behavior from sleep() 1

Status
Not open for further replies.

jjschwartz

Technical User
Feb 3, 2004
23
US
I'm trying to 'flash' a statictext label as an alert to the user.

procedure form1.flash;
begin
statictext1.color := clBlack;
statictext1.font.color := clWhite;
beep;
sleep(2000);
end;

(I've left out the restoring of the colors for this thread.)

When this code runs, it beeps, the computer sleeps for 2 seconds, and *then* the statictext changes color.

By trial-and-error, when I added application.processmessages;
after the color change but before the sleep, it worked fine. It's as if the computer puts the color change on its list of things to do but merrily goes to sleep before it's done unless it's forced too.

Can anyone explain this behavior? Why does the sleep procedure even get called before the color change is finished?

TIA,
Jeffrey Schwartz
 
It's because by changing the label's visual appearance, no repaint of the label has occurred. Repaints happen when your application is idle, because only then will it respond to messages that it needs to be repainted (or unless you call Application.ProcessMessages).

So without the ProcessMessages, the label gets it's colour changed, the control automatically sends a message to ask for it to be repainted, it goes to sleep for 2 seconds, the procedure ends, the application goes into idle mode, the application processes the messages waiting for it in the queue, which includes repainting the control.
 
don't use sleep with long timeouts in the main VCL thread. Sleep command effectivly "suspends" your thread, this means that no message processing will be done. In your case, I would use a TTimer object which doesn't have this sort of behaviour. Use sleep with long timeouts in other threads than the main VCL (visual) thread.

cheers

--------------------------------------
What You See Is What You Get
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top