Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does Application->ProcessMessage do?

Tags:

c++

c#

I'm converting a C++ application to C# and I've come across this single line of code in numerous places in the codebase:

Application->ProcessMessages()

I found this link: http://docs.embarcadero.com/products/rad_studio/delphiAndcpp2009/HelpUpdate2/EN/html/delphivclwin32/Forms_TApplication_ProcessMessages.html

Which says ProcessMessages() does the following:

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.

But what is a "message" and what is the "message queue"? How do messages get added? What sort of processing is done?

I can't seem to find any concrete explanations as to what this does, which is important because I don't know what would be the C# / .NET equivalent of this method call, or is it its functionality in some shape or way just baked into .NET and I don't need to do anything?

Tagged question with C# since I'm most curious about its .NET equivalent, but the real intent of the thread is simply to understand what this does as far as C++ is concerned.

like image 542
sab669 Avatar asked Apr 16 '26 15:04

sab669


2 Answers

The .NET equivalent is Application.DoEvents().

The message pump is the Win32 messaging system that is actually the communication mechanism of Windows itself. If you move your mouse, click a button, etc. a message is sent. A party (your application, Windows itself, etc) capable handles it, and the next message is handled. The message pump keeps sending through message that are put in the queue.

Application.DoEvents() will give your synchronous code the chance to handle those events, but it is better to make your code asynchronous so the UI thread doesn't get blocked and the messages can be handled normally without delay.

Don't use Application.DoEvents() unless you really know what you are doing and know it can bring trouble.

like image 121
Patrick Hofman Avatar answered Apr 19 '26 03:04

Patrick Hofman


ProcessMessage function allows the application, to process queued windows messages.

Windows work through messages, each event (mouse move, click, key press, redraw, etc) is a message sent to the window. All this process in .net is handled automatically so you don't have to worry on coding a WNDPROC for each window.

So, I believe you will have that line of code inside a loop of heavy work. For what? to leave time to the interface to refresh itself.

In .net you can use the Application.DoEvents() function as it will effectively call the ProcessMessages function. Source.

like image 23
Gusman Avatar answered Apr 19 '26 03:04

Gusman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!