C++Builder How To Use ProcessMessages Method In Windows Applications

FireWind

Свой
Регистрация
2 Дек 2005
Сообщения
1,957
Реакции
1,199
Credits
4,009
How To Use ProcessMessages Method In Windows Applications
By Yilmaz Yoru September 23, 2021

How can I use ProcessMessages method? What about if I have loop and the app’s GUI becomes unresponsive at runtime while the loop is running? How can I receive clicks on the components when I have a loop? How can I receive user inputs during code with heavy iterations? Let’s answer these questions.

What is the ProcessMessages Method?​

Для просмотра ссылки Войди или Зарегистрируйся Method (Vcl::Forms::TApplication:РrocessMessages) is a VCLmethod that Interrupts the execution of an application so that it can process the message queue. In other words ProcessMessages can be used to receive and process form and application events. This is very useful when you have heavy calculations since you can let the user perform processes on the screen.

Call ProcessMessages to permit the application to, as the name implies, process messages that are currently waiting in the message queue. ProcessMessages cycles the Windows message loop until it is empty and then returns control to the application. Note that 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.

What is the syntax of the ProcessMessages Method?​

Here is the syntax of the ProcessMessages method,
C++:
void __fastcall ProcessMessages();

What is a simple example of using the C++ ProcessMessages Method?

C++:
Application->ProcessMessages();

Here is the full example of using the C++ ProcessMessages Method​

C++:
#include <vcl.h>
#pragma hdrstop
 
#include "ProcessMessages_Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
 
bool loop = true;
unsigned int count;
 
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)      : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)  // START BUTTON
{
    do
    {
 count++;
 // do calculations here
 
 // receive and do form events
 Application->ProcessMessages();
 
    } while(loop);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)  // STOP BUTTON
{
 loop = false;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender)  // INFO BUTTON
{
 Memo1->Lines->Add(count);
}
In these kind of heavy calculations maybe you can do hundred or thousand iterations before you check the queue with this method which may speedup your calculations. If you want to draw or print results in any step of a long-time iterations, you can use this ProcesMessages method to process all pending operations on the form’s visuals.

Another method to do heavy calculations is to perform calculations in a Для просмотра ссылки Войди или Зарегистрируйся that means you can handle messages without calling ProcessMessages You can also use TTask and other multi-processing methods to reduce time and to make your application much more fluent.

Note that there are Для просмотра ссылки Войди или Зарегистрируйся method that interrupts the execution of an application while Windows processes a message in the Windows message queue, and Для просмотра ссылки Войди или Зарегистрируйся event that occurs when the application receives a Windows message.