问题
I'm using c++ builder (bcb6) and on:
FormShow
event there is:
Application->ProcessMessages
I would like to know what exactly the responsibility of:
Application->ProcessMessages
What exactly it did? and when we shall use by that? when it can cause exp.?
Thanks!
回答1:
The BDS 2006 IDE help states for
Application->ProcessMessagesthis:Interrupts the execution of an application so that it can process the message queue.
Call
ProcessMessagesto permit the application to process messages that are currently in the message queue.ProcessMessagescycles the Windows message loop until it is empty, and then returns control to the application.Neglecting message processing affects only the application calling
ProcessMessages, not other applications. In lengthy operations, callingProcessMessagesperiodically allows the application to respond to paint and other messages.ProcessMessagesdoes not allow the application to go idle, whereasHandleMessagedoes.so what for it is?
It allows to respond to Windows messages in case your app is blocking normal WindProc operation (inside VCL). For example if you got some lengthy computation on some event that takes minutes the application would freeze (can not click,move,resize,redraw,... until operation is done). If you once in a time call
ProcessMessagesfrom that long loop (timers would also not work during that time) that will allow to make your app responsive during this time... so it will not freeze.I usually use threads or OnIdle event instead for such computations so the main App is not blocked at all.
I am reluctant to believe that
OnShowis called during such blocking. I would place theProcessMessagesinside the computation that blocks the App (if the computations is inside theOnShowthen it is OK otherwise it would be useless. AnywayOnShowis called only if your Form is turning toVisibledo not mistake it forOnActivateorOnPaint.small example
Create empty form app and place 2 buttons in it (
btStart,btStop) then create on click event for them as following://--------------------------------------------------------------------------- bool go=false; //--------------------------------------------------------------------------- void __fastcall TForm1::btStartClick(TObject *Sender) { int i=0; for (go=true;go;) { Caption=i; i++; Application->ProcessMessages(); Sleep(100); } } //--------------------------------------------------------------------------- void __fastcall TForm1::btStopClick(TObject *Sender) { go=false; } //---------------------------------------------------------------------------When you start app and click
btStartit will start incrementing integer inCaptionfield of theForm1and stop when you clickbtStop. during counting the App is still responsive (can click,move,resize,...). You need to stop before closing App is possible (destructors wait for returning from all events). if you rem out theApplication->ProcessMessages();then the App will count but will never stop because you can not click onbtStopdue to the freeze. To close click on the IDE and press CTRL+F2.
Hope it clears things a bit.
来源:https://stackoverflow.com/questions/34997097/processmessages-on-onshow-event-c-builder