title: Unexpected behaviour of System.Windows.Window.ShowDialog() when no other windows are open. Any idea why?
body: <p>I picked up on this (with some effort) when my WPF MVVM application tried to show two consecutive error dialog windows before the main window was launched: After OKing the first window, the application went into a loop and the second error dialog never showed up.</p>
  
  <p>I fixed the problem, but I was hoping someone could enlighten me as to why this happened.</p>
  
  <p>It seems that, if there are no non-modal open windows, if one dialog window has been closed, all new dialog windows are immediately closed, without displaying.</p>
  
  <p>Its very easy to reproduce, so here is some highly conceited code to illustrate the problem. This code is complete, so using just this, you should be able to reproduce it.</p>
  
  <p>Create a Window control for the dialog window, with no code behind, and just the following XAML:</p>
  
  <pre><code>&lt;Window x:Class="ForumExampleShowDialogIssue.OKDialogWindow"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      Title="OKDialogWindow" Height="300" Width="300"&gt;
  &lt;StackPanel&gt;
      &lt;TextBlock Text="This is a Window with a single button. The button is set to Cancel, so it closes the window."
                 TextWrapping="Wrap"
                 Margin="5"/&gt;
      &lt;Button Content="OK" IsCancel="True" IsDefault="True"
              Margin="5"/&gt;
  &lt;/StackPanel&gt;
  </code></pre>
  
  <p></p>
  
  <p>Next, use the standard WPF <code>App</code> class, with nothing new in the XAML, but with the following in the code behind:</p>
  
  <pre><code>protected override void OnStartup(StartupEventArgs e)
  {
      base.OnStartup(e);
  
      OKDialogWindow alwaysOpen = new OKDialogWindow();
      alwaysOpen.Show();
  
      while (true)
      {                
          OKDialogWindow dialogWindow = new OKDialogWindow();
          Console.WriteLine("Before show");
          dialogWindow.ShowDialog();
          Console.WriteLine("After show");
      }
  }
  </code></pre>
  
  <p>Delete the MainWindow.XAML if it exists, and remove the reference to it from the App.XAML header.</p>
  
  <p>Run. (the program, not like Forest).</p>
  
  <p><strong>This works as expected.</strong> The <code>alwaysOpen</code> window remains open, while one after the other <code>dialogWindow</code> instances appear in dialog mode, closing when <code>OK</code> is clicked, then showing the next one.</p>
  
  <p><strong>HOWEVER, this breaks when you change <code>OnStartup</code> to the following:</strong></p>
  
  <pre><code>protected override void OnStartup(StartupEventArgs e)
  {
      base.OnStartup(e);
  
      while (true)
      {                
          OKDialogWindow dialogWindow = new OKDialogWindow();
          Console.WriteLine("Before show");
          dialogWindow.ShowDialog();
          Console.WriteLine("After show");
      }
  
  }
  </code></pre>
  
  <p>When there is no constantly open window, the first dialog window is the only one that works. After that, countless "Before show" and "After show" messages are printed to the console, but no new dialog windows appear - <strong>they are closed automatically as soon as they are shown.</strong></p>
  
  <p><strong>Surely this can't be the intended behaviour? Do you get the same result? Any idea why this happens?</strong></p>