Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does a modal dialog's message-pump interact with the main application message-pump?

Tags:

winapi

mfc

My understanding is any modal dialog automatically has its own message-pump, running on a thread dedicated to that dialog - is that right?

If so, how does a modal dialog's existence affect the main application's message loop? Do both run in parallel, does one take priority?

I have a situation where a modal dialog seems to get stuck for several seconds waiting for something, and wondered if it's possible the dialog is forced to wait until the main application thread is not busy?

like image 441
Mr. Boy Avatar asked Sep 18 '25 00:09

Mr. Boy


1 Answers

As IInspectable explained, the modal dialog will run in the same thread as the caller. Therefore if you run the dialog from the main UI thread that has your main message loop, you'll end up with a nested message loop. The stack would look something like:

WinMain
    YourMainMessageLoop
        DispatchMessage
            SomeMessageHandler
                DoModal

and DoModal spins in its own GetMessage/TranslateMessage/DispatchMessage loop. The main message loop (YourMainMessageLoop in the sample stack above) is "active" in the sense that it's still running, but it's blocked by the dialog's message loop. Execution won't return to YourMainMessageLoop until DoModal exits.

Note that even if you're within the modal dialog's message loop, your other windows will still handle messages because GetMessage and DispatchMessage will still retrieve and direct messages to those windows and invoke their WndProcs.

like image 187
jamesdlin Avatar answered Sep 21 '25 09:09

jamesdlin