title: Does a WPF Application Actually Need Application.Run?
body: <p>So far, I had assumed that a call to <a href="http://msdn.microsoft.com/en-us/library/ms597010%28v=vs.110%29.aspx" rel="nofollow noreferrer"><code>System.Windows.Application.Run</code></a> in a WPF application would start its main message loop and thus provide the UI with its "life". At least, that is what happens in <a href="http://msdn.microsoft.com/en-us/library/ms157900%28v=vs.110%29.aspx" rel="nofollow noreferrer">WinForms</a> and <a href="http://docs.go-mono.com/index.aspx?link=M%3aGtk.Application.Run%28%29" rel="nofollow noreferrer">Gtk#</a>, and the same thing seems to be suggested for WPF in <a href="https://stackoverflow.com/questions/18182809/wpf-application-message-loop-and-postthreadmessage">this question</a>, <a href="http://www.ageektrapped.com/blog/the-wpf-application-class-overview-and-gotcha/" rel="nofollow noreferrer">that blogpost</a>, and ultimately, the <a href="http://msdn.microsoft.com/en-us/library/ms597010%28v=vs.110%29.aspx" rel="nofollow noreferrer">docs</a> themselves (indirectly, via <a href="http://msdn.microsoft.com/en-us/library/system.windows.threading.dispatcher.run(v=vs.110).aspx" rel="nofollow noreferrer"><code>Dispatcher.Run</code></a>).</p>
  
  <p>The <a href="http://msdn.microsoft.com/en-us/library/ms597010%28v=vs.110%29.aspx" rel="nofollow noreferrer">documentation for WPF's <code>Application.Run</code></a> emphasizes how important it is to invoke <code>Run</code>:</p>
  
  <blockquote>
    <p>Run is called to start a WPF application. (...) if you define your Application using code, you will need to explicitly call Run.</p>
  </blockquote>
  
  <p>It then presents an example that tries to be compelling in its demonstration of how necessary the invocation of <code>Run</code> is. And indeed - commenting out the line</p>
  
  <pre><code>app.Run();
  </code></pre>
  
  <p>from that example results in an application that just displays a console window for a moment and then quits immediately.</p>
  
  <p>But, well, conveniently, the window in that example is created and displayed in the <a href="http://msdn.microsoft.com/en-us/library/system.windows.application.onstartup(v=vs.110).aspx" rel="nofollow noreferrer"><code>OnStartup</code> method</a> of the <code>Application</code> object, which gets invoked after calling <code>Run</code>. After moving the window-related code into the <code>Main</code> method, the window is at least displayed for a moment:</p>
  
  <pre><code>[STAThread]
  public static void Main()
  {
      CustomApplication app = new CustomApplication();
      //app.Run();
  
      Window window = new Window();
      window.Show();
  }
  </code></pre>
  
  <p>Obviously, that is because with <code>window</code> being non-modal, execution continues right after the window is displayed and the application reaches its end right away. So, if we show <code>window</code> modally, things look better:</p>
  
  <pre><code>[STAThread]
  public static void Main()
  {
      //CustomApplication app = new CustomApplication();
      //app.Run();
  
      Window window = new Window();
      window.ShowDialog();
  }
  </code></pre>
  
  <p>Now, the window is displayed just as usual. Note that I have meanwhile commented out not only the <code>Run</code> invocation, but the instantiation of the <code>Application</code> object altogether. Checking with some stepwise execution confirms that <a href="http://msdn.microsoft.com/en-us/library/system.windows.application.current(v=vs.110).aspx" rel="nofollow noreferrer"><code>Application.Current</code></a> is always <code>null</code>, so there is no <code>Application</code> instance throughout the lifetime of this application and <code>Application.Run</code> is effectively never called - yet, the application appears to be responsive.</p>
  
  <p><strong>Does <code>ShowDialog</code> perform its own invocation of <code>Dispatcher.Run</code>? If so, why the insistence in the docs to call <code>Application.Run</code>? Is there anything else going to break that I didn't encounter in this minimal test if I do not have an <code>Application</code> object or an invocation of <code>Application.Run</code> in my code?</strong></p>
  
  <p><em>Some context: I am in a situation where by default, there is no <code>Application</code> instance around (launching a new <a href="http://msdn.microsoft.com/en-us/library/system.appdomain%28v=vs.110%29.aspx" rel="nofollow noreferrer"><code>AppDomain</code></a> and serving my WPF-based GUI from there - <code>Application.Current</code> is <code>null</code> by default in the secondary <code>AppDomain</code>, and I am bypassing the default entry point in my primary <code>AppDomain</code> as I do not want the primary <code>AppDomain</code> to host the main window, hence I have to decide whether or not to call <code>Application.Run</code> myself there). Therefore, I am attempting to find out whether calling <code>Application.Run</code> can cause any problems, or whether</em> not <em>calling it will be painful in any way.</em></p>