Date: | February 28, 2005 / year-entry #48 |
Tags: | code;modality |
Orig Link: | https://blogs.msdn.microsoft.com/oldnewthing/20050228-00/?p=36343 |
Comments: | 14 |
Summary: | Earlier we saw the importance of setting the right owner window for modal UI. It is also important, when manipulating a window, to respect its modality. For example, consider the program we ended up with last time, the one which calls the MessageBox function to display a modal dialog. If we wanted to get that... |
Earlier we saw the importance of setting the right owner window for modal UI. It is also important, when manipulating a window, to respect its modality. For example, consider the program we ended up with last time, the one which calls the Respect the modality of a window. If it is disabled, don't try to get it to do things; it's disabled because it doesn't want to do anything right now. You can go hunting for its modal pop-up and talk to that pop-up. (Unless, of course, that pop-up is itself disabled, in which case you get to keep on hunting.) |
Comments (14)
Comments are closed. |
In what situation might one want to do this?
Is this why DDE is never fun when you have modal UI (particularly at startup)?
Ben Hutchings: Here are some people who came up with scenarios.
http://groups-beta.google.com/group/comp.os.ms-windows.programmer.misc/msg/52ef7ee60cc49e1d
http://groups-beta.google.com/group/comp.lang.pascal.borland/msg/a1f5b6837a83328f
http://groups-beta.google.com/group/comp.os.ms-windows.programmer.tools.mfc/msg/4085514837393c57
Jonathan Payne: This is just one of the many reasons why DDE is never fun…
I know it’s anthropomorphising but it’s not so much a case of windows are disabled because they don’t want to do anything right now – it’s more the case that some action they’ve caused would prefer that the parent window be disabled.
Keep on writing Raymond – your blog is my number 1 read each morning.
In http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/windowing/windows/windowfeatures.asp
"The system automatically destroys an owned window when its owner is destroyed."
And as you said, we should set the owner of the pop up (or message box) properly. So we don’t seem to have a problem if WM_CLOSE is sent to (and processed by) the owner of the pop up (or message box). They both should be destroyed and thus leaving no stranded window (main or pop up/message box).
Am I missing something?
Clearly something is wrong or the people in the messages I linked to are hallucinating. I already mentioned two reasons in the article.
Raymond: In the first scenario, presumably EnumWindows temporarily blocks destruction of the windows whose handles it passes to the callback, so that the handles are guaranteed to be valid, but does it also block creation and destruction of pop-up windows? If not, there seems to be a race condition in between use of GetLastActivePopup() and processing of WM_CLOSE. The second and third scenarios seem to me to be cases where the programmer needs to fix the structure of the application.
EnumWindows blocks nothing. If it did, imagine the havoc you could cause by calling EnumWindows and then calling MessageBox in your callback.
In other words, one cannot send WM_CLOSE blindly around. Problem is that MSDN didn’t warn about that at all in the description of WM_CLOSE. Q178893 mentions that WM_CLOSE may not terminate the process. IMHO, since it isn’t forbidden to send WM_CLOSE to a disabled window, applications should be able to handle this situation correctly.
Raymond: So does that mean that any program that uses another thread’s window handles has unavoidable race conditions?
"Does that mean that any program that uses another thread’s window handles has unavoidable race conditions?"
Indeed that has always been the case in Win32 if you do this without the cooperation of the target window.
That’s kind of what I suspected. It makes me think it was a mistake to try to maintain the window messaging model in a preemptive multitasking environment.
Thanks for the interesting articles. I have read all seven. I have a situation which I think is arising from what you mentioned in part 6. However, I am not sure how to work around it. I have an MDI application in which MainFrame is also an event sink. To make things interesting, Modal dialogs are also invoked in these connection points. If MDI is displaying a modal dialog, I could get another modal dialog from connection point callback. This is fairly common scenario upon startup. Now the MDI has two modal dialogs. MainFrame is disabled and user can switch between the two modal dialogs. Is this is a valid behavior? i.e. having two modal dialogs at a time for one owner(MainFrame). Also, one of them, if closed last, can enable MainFrame. I am not sure how to ensure
a) one modal dialog at a time, and
b) MainFrame is only enabled after all the modal dialogs are gone.
This was already discussed in part 5.