6

I have a simple question:

1- Create a new WPF project, there's startup window in it: MainWindow.xaml.

2- In this project, create a new window Window1.xaml.

3- Windows1's loaded event, let it close.

4- Put an Open button to MainWindow. Implement it's click event.

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    this.Close();
}

private void button_Click(object sender, RoutedEventArgs e)
{
    Window1 w = new Window1();
    w.Show();
}

When I start this application, VS2015's UI became to the DEBUGING MODE, then I click the close button on the right top corner of the Window, VS2015's UI back to normal mode.

Now, if I start application, click Open button, then Window1 will show quickly and closed, but, if I click the close button on the right top corner of MainWindow, things different: VS2015 will no go back to normal mode but stay in DEBUGING MODE. so to me, that means something hang there and I don't know what is it.

Is there anyone know how to fix this problem?

Toni
  • 1,555
  • 4
  • 15
  • 23
Oh My Dog
  • 781
  • 13
  • 32

2 Answers2

6

This is not an answer, but just my findings of an actually interesting observation. I did your test (opening and closing the Window) a couple times and then dumped the list of WPF windows:

foreach (Window w in Application.Current.Windows)
    Debug.WriteLine(w.GetType().FullName)

Resulting in:

WpfTest.MainWindow
Microsoft.VisualStudio.DesignTools.WpfTap.WpfVisualTreeService.Adorners.AdornerLayerWindow
Microsoft.VisualStudio.DesignTools.WpfTap.WpfVisualTreeService.Adorners.AdornerLayerWindow
Microsoft.VisualStudio.DesignTools.WpfTap.WpfVisualTreeService.Adorners.AdornerLayerWindow

WpfTap is Visual Studio's WPF debugger, that helps debug the WPF content tree.

Now, if instead of using the Loaded event, I used the ContentRendered event to close the window, it doesn't happen, and things work as normal. It's also fine if I run the .exe without debugging.

So it seems like Visual Studio attaches the WPF debugger *after* a Window's Loaded event, and if you close the window too early, it leaves the debugger component hanging around in memory.

NPras
  • 3,135
  • 15
  • 29
  • I've tried your 'It's also fine if I run the .exe without debugging', it's true. Now I totally understand why that happend. this is exactly the answer to my problem, just let everybody know it, thanks! – Oh My Dog Feb 08 '17 at 03:45
4

In the App.xaml set:

ShutdownMode="OnMainWindowClose"

This must solve the problem. I recommend reading this question.

Community
  • 1
  • 1
rmojab63
  • 3,513
  • 1
  • 15
  • 28
  • that works great! thanks man! even better than the link you referenced to me. – Oh My Dog Feb 07 '17 at 04:14
  • I'm glad I could help. About the link: I meant this part of that post that explains the reason of such a behavior: In your case, the app isn't closing because you're probably using the default OnLastWindowClose. If you set ShutdownMode to OnLastWindowClose: Windows Presentation Foundation (WPF) implicitly calls Shutdown when the last window in an application closes, even if any currently instantiated windows are set as the main window (see MainWindow). Since you're opening a new window, and not closing it, shutdown doesn't get called. – rmojab63 Feb 07 '17 at 06:40