'WPF App exits when window is newed up as a field

I start with a default WPF project, then I change the entry point in the App.xaml to be Startup instead of StartupUri, as follows:

<Application ...
            Startup="Application_Startup">
    <Application.Resources>
         
    </Application.Resources>
</Application>

Then I add a button to the MainWindow.xaml...

<Window ...>
    <Grid>
        <Button Click="Button_Click">Test</Button>
    </Grid>
</Window>

With the following code:

void Button_Click(object sender, RoutedEventArgs e)
{
    new Window
    {
        Height = 300,
        Width = 300,
    }.ShowDialog();
}

TheProblem

Now in the App.xaml.cs code I have the following:

using System.Windows;

namespace wpfCrash3
{
    public partial class App : Application
    {
        readonly MainWindow _mainWindowBad = new MainWindow();

        MainWindow _mainWindowGood;

        private void Application_Startup(object sender, StartupEventArgs e)
        {
            // Scenario 1: This fails
            _mainWindowBad.Show();

            // Scenario 2: This succeeds
            //_mainWindowGood = new MainWindow();
            //_mainWindowGood.Show();

            // Scenario 3: This also succeeds
            //new MainWindow().Show();
        }
    }
}

If you run Scenario 1 in Visual Studio with the debugger attached - it works fine.

Bug Repro Steps:

  1. CTRL+F5 / Run in VS without debugger
  2. Click the button
  3. Close the pop window
  4. TEST: Main window should stay open

Scenario 1 fails this test!

If you comment out Scenario 1, then uncomment out either Scenarios 2 or 3 - they each work fine.

Scenario 1 succeeds only when the debugger is attached.

I'm using VS2022 and I'm seeing the same behaviour for .NET6 and .NET Framework 4.6.1. Confirmed with a friend he sees the same behaviour in VS2019.

Can anyone else confirm this behaviour?

Question

Is this a bug in WPF? I spent 2 days trying to find this bug. What is going on!?

Scenario 1 - App exits immediately after closing sub window: Scenario 1 - App exits immediately after closing sub window

Scenario 2 - App stays open after closing sub window: Scenario 2 - App stays open after closing sub window



Solution 1:[1]

This seems to be a bug, you're right.

If you try it like this, it works:

public partial class App : Application
    {

        private readonly MainWindow _mainWindowBad;

        public App()
        {
            _mainWindowBad = new MainWindow();
        }

        private void Application_Startup(object sender, StartupEventArgs e)
        {
            // Scenario 1: This works now...
            _mainWindowBad.Show();

            // Scenario 2: This succeeds
            //_mainWindowGood = new MainWindow();
            //_mainWindowGood.Show();

            // Scenario 3: This also succeeds
            //new MainWindow().Show();
        }
        //this says that application exited with success even when it's closed automatically
        protected override void OnExit(ExitEventArgs e)
        {
            string[] lines =
            {
               e.ApplicationExitCode.ToString()
            };

             File.WriteAllLines("WriteLines.txt", lines);
            base.OnExit(e);
        }
    }

Hilarious behavior, not sure what is causing this, but I guess that it's runtime binding error when using field initializer.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dragos Stoica