3
votes

We have a dialog X, that can be opened by different threads (also main thread) as modal. Multiple modal X dialogs, at the same time from different threads is possible.

We want to close all X (only X) modal dialogs, if the main window handles a SC_CLOSE message.

The question is; From the main window, how can we close all modal X dialogs, when it recieves the SC_CLOSE message? Only X! So, PostQuitMessage doesn't help, because it closes whole app, we don't want to close the app, if other modal dialogs like Y, Z ... are opened.

Info: Main window can handle SC_CLOSE message, when we have the opened modal X dialog from main thread also from other threads. And X has no child modal window.

1
Is it legal to have a cross-process parent/child or owner/owned window relationship? "Yes, it is technically legal. It is also technically legal to juggle chainsaws."IInspectable
@IInspectable Our dialog is like a messagebox, so we don't have really managing problems. it seems my problem look likes: link, but i wan't do close X-dialog.Yusuf R. Karagöz
You didn't understand the issue. You are using a modal dialog, with an owner window that's owned by another thread. This scenario is only supported, if you specifically implemented it to be safe. Since you are using MFC this is not possible, since you have no access to the message dispatching code. That's baked into the MFC implementation. What you are doing is not safe and cannot be made safe.IInspectable
@IInspectable you are right, but there are always something that i can never change :). I can't change the desing anymore. i'm working on a software which is 20 years old. Until today we had never got problem due to safety. It only looks like, different threads show a custom-messagebox. Maybe all X-dialogs can be registered in a static global collection in a thread-safe way, then can i close those dialogs?Yusuf R. Karagöz
WM_CLOSE is the message sent when you click the Close button, not SC_CLOSE. And you can send it yourself too. Of course, because you have a multithreaded setup, I'm not sure what would happen if you were to tell the messagebox to close at any given point...andlabs

1 Answers

4
votes

My current solution is;

All modal X dialogs have been registered in a collection (thread-safely), during openning of them. if the main window becomes the SC_CLOSE-message, then iterating all dialog is now possible. Then use one of those 2 lines, for each dialog.

  ::PostMessage(pDlg->GetSafeHwnd(), WM_COMMAND, IDOK, 0); //end dialog with idok
  ::PostMessage(pDlg->GetSafeHwnd(), WM_CLOSE, 0, 0);      //or, close dialog

I know it is not the best solution, but hope helps someone.

Don't try to iterate all childs of main window to find dialogs. it won't help. Owner of the dialogs, doesn't mean that it is the parent of the dialogs.