title: WPF application exits immediately when showing a dialog before startup
body: <p><strong>Update</strong>: <em>I guess, what I need is to understand what is the "correct", "supported" way to show a dialog before application start in WPF.</em></p>
  
  <p>Here's the code:</p>
  
  <pre><code>    public partial class App : Application
      {
          [STAThread]
          public static void Main()
          {
              var app = new App();
              app.InitializeComponent();
  
              new DialogWindow().ShowDialog();
  
              app.Run( new MainWindow() );
          }
      }
  </code></pre>
  
  <p>The <code>DialogWindow</code> shows up as expected.<br>
  But after closing it, the application exits immediately. <code>MainWindow</code> doesn't show up at all!</p>
  
  <p>I have done some debugging and traced the problem to this:</p>
  
  <ol>
  <li>When the dialog is created, it becomes <code>app</code>'s MainWindow, since there is no MainWindow at the moment.</li>
  <li>Therefore, closing the dialog causes the application to post <code>ShutdownCallback</code> on the dispatcher queue.</li>
  <li>However, the dispatcher doesn't run long enough to execute the callback.</li>
  <li>Therefore, once <code>app.Run</code> is called subsequently, the first thing on the queue is <code>ShutdownCallback</code>, which, naturally, causes the app to close immediately.</li>
  </ol>
  
  <p>Given this analysis, there is an obvious workaround: create <code>MainWindow</code> right after <code>App</code>, thus making it <code>app</code>'s MainWindow, which would prevent <code>DialogWindow</code> from causing application closure.</p>
  
  <p><strong>However</strong>, here is what bothers me.</p>
  
  <p>First, this looks like a dirty hack to me. I mean, there is no explicit reason for creating windows in this order, and I have only found this through some debugging. This can't be the supported way.</p>
  
  <p>Second, this is clearly a bug. I mean, if creating a second window after shutdown wasn't supported explicitly, it should've thrown some <code>InvalidOperationException</code>, right?</p>
  
  <p>Thirdly, not only is this a bug, but it looks like a very naive one, something like a multithreading beginner would create.</p>
  
  <p><strong>All this leads me to believe</strong> that maybe I don't get something fundamental here? Maybe I don't make sense at all? Maybe it all should be done in some different fashion?</p>
  
  <p><strong>Here's some background:</strong><br>
  The application has to do some bootstrapping on startup. Check this and that, setup exception handlers, logging - you know, the usual stuff. In this process, it may become necessary to ask the user for some help - which is what the dialog is for.</p>
  
  <p>I absolutely don't want to put all that in some kind of state machine that executes on <code>MainWindow.IsVisibleChanged</code> or something like that. I would like to keep it really simple, short and straightforward - the way bootstrap code is supposed to be, so that it's easy to spot bugs with a naked eye.</p>