The World’s Largest Online Community for Developers

'; wpf - Who owns the process / main thread? - LavOzs.Com

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();

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 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

What is the difference between a process and a thread?
“implements Runnable” vs “extends Thread” in Java
How do I update the GUI from another thread?
What is a daemon thread in Java?
How can I use threading in Python?
Find (and kill) process locking port 3000 on Mac
WindowsFormsHost steals focus on activating application even activating by application's other wpf form
Xcode process launch failed: Security
C# WPF How to close a new window correctly?