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!
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, calling ProcessMessages
periodically allows the application to respond to paint and other messages.
ProcessMessages
does not allow the application to go idle, whereas HandleMessage
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 the ProcessMessages
inside the computation that blocks the App (if the computations is inside the OnShow
then it is OK otherwise it would be useless. Anyway OnShow
is called only if your Form is turning to Visible
do not mistake it for OnActivate
or OnPaint
.
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 in Caption
field of the Form1
and stop when you click btStop
. 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 the Application->ProcessMessages();
then the App will count but will never stop because you can not click on btStop
due to the freeze. To close click on the IDE and press CTRL+F2.
Hope it clears things a bit.