title: Catch unhandled exceptions from any thread
body: <h2>Edit</h2>
  
  <p>The answers to this question where helpful thanks I appreciate the help :) but I ended up using: <a href="http://code.msdn.microsoft.com/windowsdesktop/Handling-Unhandled-47492d0b#content" rel="noreferrer">http://code.msdn.microsoft.com/windowsdesktop/Handling-Unhandled-47492d0b#content</a></p>
  
  <hr>
  
  <h2>Original question:</h2>
  
  <p>I want to show a error message when my application crashes. </p>
  
  <p>Currently I have:</p>
  
  <p><code>App.xaml</code>:</p>
  
  <pre><code>&lt;Application x:Class="WpfApplication5.App"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        DispatcherUnhandledException="App_DispatcherUnhandledException"  // &lt;----------------
        StartupUri="MainWindow.xaml"&gt;
      &lt;Application.Resources&gt;             
      &lt;/Application.Resources&gt;
  &lt;/Application&gt;
  </code></pre>
  
  <p>Code-behind of <code>App.xaml</code>:</p>
  
  <pre><code>namespace WpfApplication5
  {        
      public partial class App : Application
      {
          private void App_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
          {
              MessageBox.Show("Caught Unhandled Exception!");
          }
      }
  }
  </code></pre>
  
  <p>That solution works great when the error occurs on the main thread. Now my problem is how will I be able to catch errors that happen on a different thread also? </p>
  
  <p>In other words: when I press this button I am able to catch the exception: (<code>App_DispatcherUnhandledException</code> gets called!)</p>
  
  <pre><code>    private void button1_Click(object sender, RoutedEventArgs e)
      {            
          int.Parse("lkjdsf");
      }
  </code></pre>
  
  <p>But I am not able to catch this exception:</p>
  
  <pre><code>    private void button1_Click(object sender, RoutedEventArgs e)
      {
          Task.Factory.StartNew(() =&gt;
          {
              int.Parse("lkjdsf");
          });
      }
  </code></pre>
  
  <p>How will I be able to catch all exceptions regardless if they happen on the main thread or not? </p>