In this post, you’ll learn how to use the ProcessMessages method, What about if I have a 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?
By learning how to use ProcessMessage Method in Windows applications, it will help you to easily build C++ applications using the C++ IDE.
Table of Contents
What is the ProcessMessages Method?
ProcessMessages Method (Vcl::Forms::TApplication::ProcessMessages) 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,
1 2 3 |
void __fastcall ProcessMessages(); |
What is a simple example of using the C++ ProcessMessages Method?
1 2 3 |
Application->ProcessMessages(); |
Here is the full example of using the C++ ProcessMessages Method
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
#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 IdleLoop 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 HandleMessage method that interrupts the execution of an application while Windows processes a message in the Windows message queue, and OnMessage event that occurs when the application receives a Windows message.
C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.
There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise versions of C++ Builder and there is a trial version you can download from here.