1

I have the following scenario in a multi-process C# WPF application:

MainApplication has a placeholder to contain child processes' HWNDs.

Using SetParent and some more windows functions (P/Invoke, Windows API) I can set child applications to live inside main application without letting the user know they are 2 processes. Until now everything works good.

The problem is that every time I open a Modal Dialog from the child process, the modal dialog is actually not modal. Instead If I run the child application (same exe) standalone, the modal dialog works correctly.

Using Spy++ I realized that the owner of the modal dialog is the Desktop if running as a child process (not working as intended), or the correct application window, if running standalone (working as intended).

I also tried to use ::SetParent on the dialog, but nothing change, It seems that when the dialog is created from a hosted process, WPF can't set it's parent correctly.

Am I doing something wrong?

EDIT: of course I tried to set dialog's owner using wpf methods (also WindowInteropHelper)

nemenos
  • 877
  • 1
  • 10
  • 23
  • Yes, you're doing something wrong. Using `SetParent` to host another process inside yours is a hack - a dirty awful hack with all kinds of nasty quirks, oddities, pitfalls, bugs, and potential for dozens of things to go wrong - wonky modal dialogs are just the tip of the iceberg. Don't write applications like this - whatever your problem is, this is not the solution. – J... Jun 12 '15 at 09:41
  • http://blogs.msdn.com/b/oldnewthing/archive/2013/04/12/10410454.aspx – J... Jun 12 '15 at 09:44
  • 1
    http://stackoverflow.com/a/3045880/327083 This is delphi, not c#, but the principle remains the same. – J... Jun 12 '15 at 09:52
  • Ok, so, how do multi-tab browser manage multiprocess windows? – nemenos Jun 12 '15 at 10:03
  • Multitab browsers are still a single process. They have multiple windows and are heavily multithreaded, but all windows and threads belong to the same process. – J... Jun 12 '15 at 10:07
  • No, not Chrome for example. Chrome is multiprocess. A process per Tab. Or something like that – nemenos Jun 12 '15 at 10:07
  • 1
    Ok, Chrome is a special exception. To answer the "how do they do it" question is not straightforward at all. I'm not saying that hosting multiple processes is impossible - it's just exceptionally difficult to get it to work right. Chrome has a massive codebase to manage all of the nuances of those processes - very deep micromanagement of low level WinAPI, very careful error and exception handling, etc. – J... Jun 12 '15 at 10:16
  • 1
    Further, Chrome is a special case where the hosted processes are all part of the controlled codebase and are written to *expect* to be hosted in a parent process. This can't be said for almost any other random application you may try to graft into your own program. http://stackoverflow.com/q/2019500/327083 In almost any other conceivable situation it is usually highly advisable to seriously reconsider the architecture when faced with a desire to host one process inside another. – J... Jun 12 '15 at 10:17
  • 1
    For example, other things to consider : http://stackoverflow.com/q/170800/327083 ; http://stackoverflow.com/q/4124025/327083 ; http://stackoverflow.com/q/3459874/327083 – J... Jun 12 '15 at 10:23
  • 1
    Even Jon Skeet says "be careful!" : http://stackoverflow.com/a/5191023/327083 – J... Jun 12 '15 at 10:24
  • For the record, IE is multi-process as well. It wouldn't surprise me to find most if not all browser vendors moving to such a design. But it _is_ complicated to implement; not for the faint of heart. You may require some inter-process negotiation to make sure all the windows cooperate with each other, including display dialogs modally. Things like Chrome and IE definitely are _not_ trying to run multiple processes that all are ignorant of each other. – Peter Duniho Jun 13 '15 at 04:43
  • I tried to study a little bit of Chromium architecture, which is open source and documented. Only one process manages UI, all the other ones are just calculation ones. So I think it's definitely not a good design, and if you can, you should avoid doing that. – nemenos Jun 17 '15 at 13:20

0 Answers0