问题
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->ProcessMessages
this:Interrupts the execution of an application so that it can process the message queue.
Call
ProcessMessages
to permit the application to process messages that are currently in the message queue.ProcessMessages
cycles 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, callingProcessMessages
periodically allows the application to respond to paint and other messages.ProcessMessages
does not allow the application to go idle, whereasHandleMessage
does.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
ProcessMessages
from 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
OnShow
is called during such blocking. I would place theProcessMessages
inside the computation that blocks the App (if the computations is inside theOnShow
then it is OK otherwise it would be useless. AnywayOnShow
is called only if your Form is turning toVisible
do not mistake it forOnActivate
orOnPaint
.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
btStart
it will start incrementing integer inCaption
field of theForm1
and 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 onbtStop
due 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