ProcessMessages on OnShow Event c++ builder

一世执手 提交于 2019-12-24 14:52:10

问题


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:


  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, 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.

  2. 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.

  3. 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.



来源:https://stackoverflow.com/questions/34997097/processmessages-on-onshow-event-c-builder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!