2

I created new WPF Application witn two windows.

  1. MainWindow.xaml
  2. Window1.xaml

Added one button in MainWindow.xaml and wrote the following code in the click event of the button:

Window1 w = new Window1();
w.Show();

I clicked that button 2-3 times and it opened multiple instances of Window1.

Everything fine till here.

Now, I closed MainWindow and it did not close the instances of Windows1.

I was thinking that since MainWindow is the one that owns the Process and if it closes, rest of the child windows will close automatically.

I did the same project in WinForms application and the result was completely different. On closing the MainWindow, it did close rest of the windows.

Am i not understanding the concept clearly? Can someone tell me who owns the process or the main thread in WPF?

H.B.
  • 166,899
  • 29
  • 327
  • 400
Asdfg
  • 11,362
  • 24
  • 98
  • 175

1 Answers1

5

The other windows are not child windows of your MainWindow unless you set window.Owner = mainWindow;

Further there is the Application.MainWindow property which sets which window actually is treated as "the" main window. This affects the Application.ShutdownMode if set to OnMainWindowClose.


Excerpt from the Window.Owner reference:

When a child window is opened by a parent window by calling ShowDialog, an implicit relationship is established between both parent and child window. This relationship enforces certain behaviors, including with respect to minimizing, maximizing, and restoring.

When a child window is created by a parent window by calling Show, however, the child window does not have a relationship with the parent window. This means that:

  • The child window does not have a reference to the parent window.

  • The behavior of the child window is not dependent on the behavior of the parent window; either window can cover the other, or be minimized, maximized, and restored independently of the other.

To allow you to create a relationship between a child window and a parent window, Window supports the notion of ownership. Ownership is established when the Owner property of a window (the owned window) is set with a reference to another window (the owner window).

The application root class: System.Windows.Application

WPF Threading Model

H.B.
  • 166,899
  • 29
  • 327
  • 400
  • Ok. So i added w.Owner = this; in the code and now it behaves the way i thought it shud be. This leaves me a big thought: What if i have an application with multiple windows and something goes wrong and the mainwindow gets closed. Other windows will hang in there orphan? Who is responsible to terminate the process? In WinForms app, i did not specify the owner but it still behaved differently. Why there is a disconnect here? – Asdfg Apr 22 '11 at 13:47
  • Seems like they just decided to implement a different relationship logic. In Winforms a parent-child relationship was formed automatically, in WPF it has to be done manually. If "something goes wrong" rarely one window will close on its own, normally the whole application will terminate; WPF applications normally have an `App` class which is the root of the application. – H.B. Apr 22 '11 at 14:20
  • worked on it a bit more. In WinForms application, if i dont specify the owner, then child windows have owner as null. still CLR keeps track of windows opened from the main window and closes them when the main window is closed but the same thing doesnt happen in WPF application. If i throw Runtime exception in the WPF application, it does close all the windows created from the main window. Why is is behaving differently? I am confused. – Asdfg Apr 22 '11 at 15:11
  • Well, `WPF != Winforms`, if everything was the same there would be no point in having both. – H.B. Apr 22 '11 at 15:13
  • True but if i am understanding the problem correctly then it can lead to some serious Memory leak. – Asdfg Apr 22 '11 at 15:39