title: Difference between using Application.StartupUri and handling Startup event in WPF?
body: <p>I've developed a WPF application which uses an advanced 3rd party UserControl; the UserControl shows an interactive map. If I open the main window of the application by specifying <code>StartupUri="MainWindow"</code> in XAML, or if I start the application from code using <code>app.Run(new MainWindow())</code>, then everything works fine. However, if I instead handle the Application Startup event this way</p>
  
  <pre><code>private void Application_Startup(object sender, StartupEventArgs e)
  {
      new MainWindow().Show();
  }
  </code></pre>
  
  <p>then I get the following exception when I start interacting with the map:</p>
  
  <blockquote>
    <p>Cannot use a DependencyObject that belongs to a different thread than its parent Freezable.</p>
  </blockquote>
  
  <p>Why might this happen - is there some difference in which thread does what between these different ways of starting the application? Here is a partial stack trace for the exception:</p>
  
  <blockquote>
  <pre><code>at System.Windows.Freezable.EnsureConsistentDispatchers(DependencyObject owner, DependencyObject child)
  at System.Windows.Freezable.OnFreezablePropertyChanged(DependencyObject oldValue, DependencyObject newValue, DependencyProperty property)
  at System.Windows.Media.RenderData.PropagateChangedHandler(EventHandler handler, Boolean adding)
  at System.Windows.Media.DrawingVisual.RenderClose(IDrawingContent newContent)
  at System.Windows.Media.RenderDataDrawingContext.DisposeCore()
  at System.Windows.Media.DrawingContext.System.IDisposable.Dispose()
  at some3rdPartyClass.BuildVisual(Brush fg, Brush bg)
  </code></pre>
  </blockquote>