Click here to Skip to main content
15,889,853 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I was debugging an application and placed calls to message boxes (as break points) in the WinMain. At some point I prevented the "while (GetMessage())" loop from executing, but my application worked fine! To test it further, I completely removed the loop:
C#
#pragma argsused
int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
MSG msg;
if (!hPrevInstance)
    if (!InitApplication(hInstance)) return 0;
if (!InitInstance(hInstance, nCmdShow)) return 0;
/*
while (GetMessage(&msg, NULL, 0, 0)) {
    TranslateMessage(&msg);
    DispatchMessage(&msg);
    };
*/
MessageBox(NULL, "About to return", "Debug", MB_OK);
return msg.wParam;
};


but the application still worked (as long as I kept that message box alive) - and not just "worked" - like showing a simple window - it maintained serial communications, data acquisition hardware, file creation, registry access, graphics and so on - it all worked as usual.
I have always been sure that all the messages in the main thread are going through that GetMessage() loop, and indeed, it is easy to verify that by putting a trap inside the loop. But with the entire loop commented out - I do not understand what is going on.

//==================================
I am adding this part for future readers, after the solutions are accepted:
A message box in my code must be nothing but a modal dialog box created by the system, most likely in a very similar way that DialoBox() function works.
According to Win32 Online Help, "The [DialogBox] function displays the dialog box …, disables the owner window, and finally starts its own message loop to retrieve and dispatch messages for the dialog box.
When the dialog box procedure calls the EndDialog function, DialogBox destroys the dialog box, ends the message loop, enables the owner window (if previously enabled), and returns the nResult parameter …"

Now, the call to MessageBox() has NULL for the parent - that is why my main window (created in the call to InitInstance) does not get disabled. So far so good: now we have a thread and a message loop within it. The only ambiguity remains in the phrase “starts its own message loop to retrieve and dispatch messages for the dialog box – apparently, not just for the dialog box but for all windows created by the current thread.
Posted
Updated 21-Oct-11 5:36am
v2

Every thread has a message loop, but any message queue created within an application with that second value set to NULL, creates a message queue for the current thread. This means that even though you're removing your message loop (that creates the message queue when it's first called), the MessageBox() you're creating is probably internally making its own message loop, which by default creates a message loop for the thread it's operating on.
 
Share this answer
 
v2
Comments
SMD111 20-Oct-11 13:42pm    
Thank you, Albert. I thus understand that in this case the GetMessage() -> DispatchMessage() loop is only a hook in the main thread used to catch WM_QUIT and make WinMain() return?
Albert Holguin 20-Oct-11 15:23pm    
Actually, I said that wrong, every THREAD has a message loop...somewhere...the MessageBox() is probably making a message loop since he needs one anyway.
Albert Holguin 20-Oct-11 15:28pm    
I updated my solution once I thought that one through a bit more... I've had this discussion with other engineers before when discussing messaging...
SMD111 20-Oct-11 16:29pm    
Thanks again, I've been reading a lot on MSDN. To clarify my question, I'll use two citations from there:

"A simple message loop consists of one function call to each of these three functions: GetMessage, TranslateMessage, and DispatchMessage."

"An application's main thread starts its message loop after initializing the application and creating at least one window. After it is started, the message loop continues to retrieve messages from the thread's message queue and to dispatch them to the appropriate windows."

So, what is retrieving and dispatching messages in the absence of the message loop, like in my example above?
I do the same thing for simple debugging.
Now I know I'm not alone.

Anyway, a message box creates the exact same message loop as the one you commented out in your code.
That's why your app keeps working the same way.

In fact, I've seen apps from extreme coders that don't have explicit message loops.
They simply show a message box and run just the same.

Here's something to try to improve your understanding.
Create a GUI app with one button.
Use your message loop as the code of the button.
Now, each time you click the button, the app goes into a message loop that is one level deeper.
Closing the app means breaking out of each loop, which also means that you have to click the Close button several times.

Try it or just imagine it.
It works either way.

Cheers.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900