The World’s Largest Online Community for Developers
I created new WPF Application witn two windows.
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?
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
Excerpt from the
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: