title: WPF: Window.Show() - doesn't work
body: <p>I have an application that on the first run will ask the user to choose configurations, and open a FirstRunWindow. After that, it launches the MainWindow. </p>
  
  <p>Until now I had it wired up with events, and it worked perfectly. But I came to think that actually I don't need an event, and I tried to re-write the code, but I'm getting weird results. </p>
  
  <p>1) If the check is made that there is no need to run the first window, it will load the main window fine. But if the First Window ran - the MainWindow.Show() won't work (i.e. the main window won't appear). I went over with debugger - no error is thrown, it seems to have been executed fine, but doesn't appear.</p>
  
  <p>2) Changing Show to ShowDialog() doesn't solve it, and also throws an error when I call Application.Current.Shutdown().</p>
  
  <p>3) Putting a MessageBox.Show() after the MainWindow.Show() works if the first window didn't appear, throws weird error if the first window appears (A very-very long stack with the final exception being "the uri prefix is not recognized" - but this seems completely unrelated, and all because of a MessageBox ??? </p>
  
  <p>I should also note that <strong>ShutdownMode="OnExplicitShutdown"</strong>. So there's no worries regarding having windows open or not.  </p>
  
  <p>Here is how the code looked like <strong>before</strong>, with events (again, this worked):</p>
  
  <pre><code>    public static event EventHandler ContinueMain;
  
      public void StartFlow()
      {
  
          // Check if this is the first run
          CheckFirstRun();
  
          // Hook up an event
          FirstRun.ContinueMain += FirstRun_ContinueMain;
  
      }
  
      private void FirstRun_ContinueMain(object sender, EventArgs e)
      {
          ContinueMain();
      }
  
      public void ContinueMain()
      {
          // Do Stuff
          DoStuff();
  
          // Show's Main Window
          ShowMain();
      }
  
      public void CheckFirstRun()
      {
          if (condition) 
              {
                  firstRunWindow = new FirstRunWindow();
                  firstRunWindow.Show();
                  return;
              }
  
          ContinueMain();             
      }
  
      // Called when user clicks on Finish in last view in FirstRunWindow... 
      public void FirstRunFinish()
      {
          firstRunWindow.Close();
          ContinueMain?.Invoke(null, EventArgs.Empty)     
      }
  
      public void DoStuff()
      { 
          //irrelevant    
      }
  
      public void ShowMain()
      {
          var mainWindow = new MainWindow();
  
          mainWindow.Show();
  
      }
  </code></pre>
  
  <p>And here is how the code looks like now, without events but with bugs:</p>
  
  <pre><code>    public void StartFlow()
      {
          // Check if this is the first run
          CheckFirstRun();
  
          // Do Stuff
          DoStuff();
  
          // Show's Main Window
          ShowMain();
      }
  
      public void CheckFirstRun()
      {
          if (condition) 
              {
                  firstRunWindow = new FirstRunWindow();
                  firstRunWindow.ShowDialog(); // changed to ShowDialog, so that thread stops here and waits for the first window to close...
              }
  
      }
  
      public void FirstRunFinish()
      {
          firstRunWindow.Close();
      }
  
      public void DoStuff()
      { 
          //irrelevant    
      }
  
      public void ShowMain()
      {
          var mainWindow = new MainWindow();
  
          mainWindow.Show();
          // MessageBox.Show("dbg: After mainWindow.Show()");
  
      }
  </code></pre>
  
  <p>WER / EventViewer show's the following errors:</p>
  
  <pre><code>Exception Info: System.InvalidOperationException
     at System.Windows.Application.set_ShutdownMode(System.Windows.ShutdownMode)
  
  Exception Info: System.Windows.Markup.XamlParseException
     at System.Windows.Markup.XamlReader.RewrapException(System.Exception, System.Xaml.IXamlLineInfo, System.Uri)
     at System.Windows.Markup.WpfXamlLoader.Load(System.Xaml.XamlReader, System.Xaml.IXamlObjectWriterFactory, Boolean, System.Object, System.Xaml.XamlObjectWriterSettings, System.Uri)
     at System.Windows.Markup.WpfXamlLoader.LoadBaml(System.Xaml.XamlReader, Boolean, System.Object, System.Xaml.Permissions.XamlAccessLevel, System.Uri)
     at System.Windows.Markup.XamlReader.LoadBaml(System.IO.Stream, System.Windows.Markup.ParserContext, System.Object, Boolean)
     at JITBClient.App.Main()
  </code></pre>
  
  <p>here is the MessageBox Exception (same for Forms.MessageBox, and wpf MessageBox):</p>
  
  <p><a href="https://i.stack.imgur.com/WXniV.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/WXniV.png" alt="enter image description here"></a></p>
  
  <p>Any help is appreciated.</p>
  
  <p><strong>Update:</strong></p>
  
  <p>Adding <code>Application.Current.ShutdownMode = ShutdownMode.OnExplicitShutdown;</code> to the code seems to fix this, though I don't understand why the XAML version in App.xaml isn't enough... (as mentioned above, I do have <code>ShutdownMode="OnExplicitShutdown"</code> in App.xaml) If anyone can clarify if I did something wrong, or if it's a microsoft bug ???</p>