Catch DispatcherUnhandledException from other Thread<p class=MsoNormal style="margin:0cm 0cm 0pt"><font face="Times New Roman, Times, Serif">Hi,</font></p> <p class=MsoNormal style="margin:0cm 0cm 0pt"><font face="Times New Roman, Times, Serif"> </font></p> <p class=MsoNormal style="margin:0cm 0cm 0pt"><font face="Times New Roman, Times, Serif">I’m having trouble to catch the DispatcherUnhandledException from other threads than the 'main thread'. If you read the MSDN documentation<span style="">  </span>about the </font><a title="http://msdn2.microsoft.com/en-us/library/system.windows.application.dispatcherunhandledexception.aspx" href="http://msdn2.microsoft.com/en-us/library/system.windows.application.dispatcherunhandledexception.aspx"><font face="Times New Roman, Times, Serif">DispatcherUnhandledException</font></a><font face="Times New Roman, Times, Serif"> it says:</font></p> <p class=MsoNormal style="margin:0cm 0cm 0pt"><font face="Times New Roman, Times, Serif"> </font></p> <p class=MsoNormal style="margin:0cm 0cm 0pt"><font face="Times New Roman, Times, Serif"> </font></p> <p class=MsoNormal style="margin:0cm 0cm 0pt"><font face="Times New Roman, Times, Serif"><b>DispatcherUnhandledException</b> is raised by an Application for each exception that is not handled within the AppDomain that the <b>Application</b> object was created in.</font></p> <p class=MsoNormal style="margin:0cm 0cm 0pt"><font face="Times New Roman, Times, Serif"> </font></p> <p class=MsoNormal style="margin:0cm 0cm 0pt"><font face="Times New Roman, Times, Serif">The <b>DispatcherUnhandledException</b> event handler is passed a DispatcherUnhandledExceptionEventArgs argument that contains contextual information regarding the exception, including:</font></p> <ul style="margin-top:0cm" type=disc> <li class=MsoNormal style="margin:0cm 0cm 0pt;tab-stops:list 36.0pt"><font face="Times New Roman, Times, Serif">The exception (Exception).</font></li> <li class=MsoNormal style="margin:0cm 0cm 0pt;tab-stops:list 36.0pt"><font face="Times New Roman, Times, Serif">The Dispatcher from which it originated (Dispatcher).</font></li></ul> <p class=MsoNormal style="margin:1.35pt 0cm"><span style="font-size:8.5pt;color:black;font-family:Verdana"><font face="Times New Roman, Times, Serif"></font></span></p> <p class=MsoNormal style="margin:0cm 0cm 0pt"><font face="Times New Roman, Times, Serif"> </font></p> <p class=MsoNormal style="margin:0cm 0cm 0pt"><font face="Times New Roman, Times, Serif">This means that the exception also should be called from other threads than the main thread.</font></p> <p class=MsoNormal style="margin:0cm 0cm 0pt"><font face="Times New Roman"></font> </p> <p class=MsoNormal style="margin:0cm 0cm 0pt"><font face="Times New Roman">If this isn't working, what's then the best way to handle global exceptions in WPF applications?</font></p> <p class=MsoNormal style="margin:0cm 0cm 0pt"><font face="Times New Roman, Times, Serif"> </font></p> <p class=MsoNormal style="margin:0cm 0cm 0pt"><font face="Times New Roman, Times, Serif">Note, it works fine to catch the </font><a title="http://msdn2.microsoft.com/en-us/library/system.appdomain.unhandledexception.aspx" href="http://msdn2.microsoft.com/en-us/library/system.appdomain.unhandledexception.aspx"><font face="Times New Roman, Times, Serif">AppDomain.UnhandledException</font></a><font face="Times New Roman, Times, Serif">. But then its to late to handle the exception and the application is shut down.</font></p> <p class=MsoNormal style="margin:0cm 0cm 0pt"><font face="Times New Roman, Times, Serif"> </font></p> <p class=MsoNormal style="margin:0cm 0cm 0pt"><font face="Times New Roman, Times, Serif"> </font></p> <p class=MsoNormal style="margin:0cm 0cm 0pt"><font face="Times New Roman, Times, Serif">An example, raising exception from ThreadPool and from main the thread: </font></p><font color="#0000ff" size=2> <p>using</font><font size=2> System;</font></p> <p><font color="#0000ff" size=2>using</font><font size=2> System.Windows;</p></font><font color="#0000ff" size=2> <p>using</font><font size=2> System.Collections.Generic;</p></font><font color="#0000ff" size=2> <p>using</font><font size=2> System.Text;</p></font><font color="#0000ff" size=2> <p>using</font><font size=2> System.Windows.Threading;</p></font><font color="#0000ff" size=2> <p>using</font><font size=2> System.Threading;</p></font><font color="#0000ff" size=2> <p>namespace</font><font size=2> DispatcherUnhandledExceptionApplication</p> <p>{</p> <p></font><font color="#0000ff" size=2>public</font><font size=2> </font><font color="#0000ff" size=2>class</font><font size=2> </font><font color="#008080" size=2>MyApplication</font><font size=2> : </font><font color="#008080" size=2>Application</p></font><font size=2> <p>{</p> <p>[</font><font color="#008080" size=2>STAThread</font><font size=2>]</p> <p></font><font color="#0000ff" size=2>public</font><font size=2> </font><font color="#0000ff" size=2>static</font><font size=2> </font><font color="#0000ff" size=2>void</font><font size=2> Main()</p> <p>{</p> <p></font><font color="#008080" size=2>MyApplication</font><font size=2> app = </font><font color="#0000ff" size=2>new</font><font size=2> </font><font color="#008080" size=2>MyApplication</font><font size=2>();</p> <p>app.InitializeComponent();</p> <p>app.Run(); </p> <p>}</p> <p></font><font color="#0000ff" size=2>protected</font><font size=2> </font><font color="#0000ff" size=2>override</font><font size=2> </font><font color="#0000ff" size=2>void</font><font size=2> OnStartup(</font><font color="#008080" size=2>StartupEventArgs</font><font size=2> e)</p> <p>{</p> <p></font><font color="#0000ff" size=2>base</font><font size=2>.OnStartup(e);</p> <p></font><font color="#008080" size=2>Window</font><font size=2> aWindow = </font><font color="#0000ff" size=2>new</font><font size=2> </font><font color="#008080" size=2>Window</font><font size=2>();</p> <p>aWindow.ShowDialog();</p> <p>} </p> <p></font><font color="#0000ff" size=2>public</font><font size=2> </font><font color="#0000ff" size=2>void</font><font size=2> InitializeComponent()</p> <p>{</p> <p></font><font color="#008080" size=2>AppDomain</font><font size=2>.CurrentDomain.UnhandledException += </font><font color="#0000ff" size=2>new</font><font size=2> </font><font color="#008080" size=2>UnhandledExceptionEventHandler</font><font size=2>(CurrentDomain_UnhandledException);</p> <p>DispatcherUnhandledException += </font><font color="#0000ff" size=2>new</font><font size=2> System.Windows.Threading.</font><font color="#008080" size=2>DispatcherUnhandledExceptionEventHandler</font><font size=2>(App_DispatcherUnhandledException);</p> <p>}</p> <p></p> <p></font><font color="#0000ff" size=2>void</font><font size=2> CurrentDomain_UnhandledException(</font><font color="#0000ff" size=2>object</font><font size=2> sender, </font><font color="#008080" size=2>UnhandledExceptionEventArgs</font><font size=2> e)</p> <p>{</p> <p>}</p> <p></p> <p></font><font color="#0000ff" size=2>void</font><font size=2> App_DispatcherUnhandledException(</font><font color="#0000ff" size=2>object</font><font size=2> sender, System.Windows.Threading.</font><font color="#008080" size=2>DispatcherUnhandledExceptionEventArgs</font><font size=2> e)</p> <p>{</p> <p>e.Handled = </font><font color="#0000ff" size=2>true</font><font size=2>;</p> <p>System.</font><font color="#008080" size=2>Console</font><font size=2>.WriteLine(</font><font color="#800000" size=2>&quot;DispatcherUnhandledException captured!&quot;</font><font size=2>);</p> <p>}</p> <p>}</p> <p>}</p> <p> </p> <p> </p> <p> </p><font color="#0000ff" size=2> <p>using</font><font size=2> System;</p></font><font color="#0000ff" size=2> <p>using</font><font size=2> System.Collections.Generic;</p></font><font color="#0000ff" size=2> <p>using</font><font size=2> System.Text;</p></font><font color="#0000ff" size=2> <p>using</font><font size=2> System.Windows;</p></font><font color="#0000ff" size=2> <p>using</font><font size=2> System.Windows.Controls;</p></font><font color="#0000ff" size=2> <p>using</font><font size=2> System.Windows.Data;</p></font><font color="#0000ff" size=2> <p>using</font><font size=2> System.Windows.Documents;</p></font><font color="#0000ff" size=2> <p>using</font><font size=2> System.Windows.Input;</p></font><font color="#0000ff" size=2> <p>using</font><font size=2> System.Windows.Media;</p></font><font color="#0000ff" size=2> <p>using</font><font size=2> System.Windows.Media.Imaging;</p></font><font color="#0000ff" size=2> <p>using</font><font size=2> System.Windows.Shapes;</p></font><font color="#0000ff" size=2> <p>using</font><font size=2> System.Threading;</p></font><font color="#0000ff" size=2> <p>using</font><font size=2> System.Windows.Threading;</p> <p> </p></font><font color="#0000ff" size=2> <p>namespace</font><font size=2> DispatcherUnhandledExceptionApplication</p> <p>{</p> <p></font><font color="#808080" size=2>///</font><font color="#008000" size=2> </font><font color="#808080" size=2>&lt;summary&gt;</p></font><font size=2> <p></font><font color="#808080" size=2>///</font><font color="#008000" size=2> Interaction logic for Window1.xaml</p></font><font size=2> <p></font><font color="#808080" size=2>///</font><font color="#008000" size=2> </font><font color="#808080" size=2>&lt;/summary&gt;</p></font><font size=2> <p></font><font color="#0000ff" size=2>public</font><font size=2> </font><font color="#0000ff" size=2>partial</font><font size=2> </font><font color="#0000ff" size=2>class</font><font size=2> </font><font color="#008080" size=2>Window</font><font size=2> : System.Windows.</font><font color="#008080" size=2>Window</p></font><font size=2> <p>{</p> <p></font><font color="#0000ff" size=2>public</font><font size=2> Window()</p> <p>{</p> <p>InitializeComponent();</p> <p>}</p> <p></font><font color="#0000ff" size=2>void</font><font size=2> OnUIThreadButtonClicked(</font><font color="#0000ff" size=2>object</font><font size=2> sender, </font><font color="#008080" size=2>EventArgs</font><font size=2> e)</p> <p>{</p> <p>RaiseArgumentNullException();</p> <p>}</p> <p></font><font color="#0000ff" size=2>void</font><font size=2> OnThreadPoolButtonClicked(</font><font color="#0000ff" size=2>object</font><font size=2> sender, </font><font color="#008080" size=2>EventArgs</font><font size=2> e)</p> <p>{</p> <p></font><font color="#008080" size=2>ThreadPool</font><font size=2>.QueueUserWorkItem(</font><font color="#0000ff" size=2>new</font><font size=2> </font><font color="#008080" size=2>WaitCallback</font><font size=2>(</font><font color="#0000ff" size=2>delegate</font><font size=2>(</font><font color="#0000ff" size=2>object</font><font size=2> state)</p> <p>{</p> <p></font><font color="#0000ff" size=2>try</p></font><font size=2> <p>{</p> <p></font><font color="#008080" size=2>Dispatcher</font><font size=2>.CurrentDispatcher.UnhandledException += </font><font color="#0000ff" size=2>new</font><font size=2> </font><font color="#008080" size=2>DispatcherUnhandledExceptionEventHandler</font><font size=2>(CurrentDispatcher_UnhandledException);</p> <p></font><font color="#0000ff" size=2>int</font><font size=2> aDomainID = </font><font color="#008080" size=2>AppDomain</font><font size=2>.CurrentDomain.Id;</p> <p></font><font color="#008080" size=2>Thread</font><font size=2>.CurrentThread.Name = </font><font color="#800000" size=2>&quot;MyThreadPoolThread&quot;</font><font size=2>;</p> <p></font><font color="#008080" size=2>Dispatcher</font><font size=2>.CurrentDispatcher.Invoke(</font><font color="#008080" size=2>DispatcherPriority</font><font size=2>.Normal, </font><font color="#0000ff" size=2>new</font><font size=2> </font><font color="#008080" size=2>MyDlg</font><font size=2>(RaiseArgumentNullException));</p> <p>}</p> <p></font><font color="#0000ff" size=2>catch</font><font size=2> (</font><font color="#008080" size=2>Exception</font><font size=2> ex)</p> <p>{</p> <p></font><font color="#0000ff" size=2>throw</font><font size=2> ex;</p> <p>}</p> <p></font><font color="#0000ff" size=2>finally</p></font><font size=2> <p>{</p> <p></font><font color="#008080" size=2>Dispatcher</font><font size=2>.CurrentDispatcher.UnhandledException -= </font><font color="#0000ff" size=2>new</font><font size=2> </font><font color="#008080" size=2>DispatcherUnhandledExceptionEventHandler</font><font size=2>(CurrentDispatcher_UnhandledException);</p> <p>}</p> <p>}));</p> <p>}</p> <p></font><font color="#0000ff" size=2>void</font><font size=2> CurrentDispatcher_UnhandledException(</font><font color="#0000ff" size=2>object</font><font size=2> sender, </font><font color="#008080" size=2>DispatcherUnhandledExceptionEventArgs</font><font size=2> e)</p> <p>{</p> <p>e.Handled = </font><font color="#0000ff" size=2>false</font><font size=2>;</p> <p>}</p> <p></font><font color="#0000ff" size=2>delegate</font><font size=2> </font><font color="#0000ff" size=2>void</font><font size=2> </font><font color="#008080" size=2>MyDlg</font><font size=2>();</p> <p></font><font color="#0000ff" size=2>void</font><font size=2> RaiseArgumentNullException()</p> <p>{</p> <p></font><font color="#0000ff" size=2>throw</font><font size=2> </font><font color="#0000ff" size=2>new</font><font size=2> </font><font color="#008080" size=2>ArgumentNullException</font><font size=2>();</p> <p>} </p></font><font size=2> <p>}</p> <p>}</p></font> <p> </p> <p> </p> <p> </p> <p> </p> <p> </p><font color="#0000ff" size=2> <p>&lt;</font><font color="#800000" size=2>Window</font><font color="#0000ff" size=2> </font><font color="#ff0000" size=2>x:Class</font><font color="#0000ff" size=2>=</font><font size=2>&quot;</font><font color="#0000ff" size=2>DispatcherUnhandledExceptionApplication.Window</font><font size=2>&quot;</p></font><font color="#0000ff" size=2> <p></font><font color="#ff0000" size=2>xmlns</font><font color="#0000ff" size=2>=</font><font size=2>&quot;</font><font color="#0000ff" size=2>http://schemas.microsoft.com/winfx/2006/xaml/presentation</font><font size=2>&quot;</p></font><font color="#0000ff" size=2> <p></font><font color="#ff0000" size=2>xmlns:x</font><font color="#0000ff" size=2>=</font><font size=2>&quot;</font><font color="#0000ff" size=2>http://schemas.microsoft.com/winfx/2006/xaml</font><font size=2>&quot;</p></font><font color="#0000ff" size=2> <p></font><font color="#ff0000" size=2>Title</font><font color="#0000ff" size=2>=</font><font size=2>&quot;</font><font color="#0000ff" size=2>WindowsApplication17</font><font size=2>&quot;</font><font color="#0000ff" size=2> </font><font color="#ff0000" size=2>Height</font><font color="#0000ff" size=2>=</font><font size=2>&quot;</font><font color="#0000ff" size=2>97</font><font size=2>&quot;</font><font color="#0000ff" size=2> </font><font color="#ff0000" size=2>Width</font><font color="#0000ff" size=2>=</font><font size=2>&quot;</font><font color="#0000ff" size=2>300</font><font size=2>&quot;</p></font><font color="#0000ff" size=2> <p>&gt;</p> <p>&lt;</font><font color="#800000" size=2>Grid</font><font color="#0000ff" size=2>&gt;</p></font><font color="#0000ff" size=2> <p>&lt;</font><font color="#800000" size=2>Button</font><font color="#0000ff" size=2> </font><font color="#ff0000" size=2>Height</font><font color="#0000ff" size=2>=</font><font size=2>&quot;</font><font color="#0000ff" size=2>23</font><font size=2>&quot;</font><font color="#0000ff" size=2> </font><font color="#ff0000" size=2>Click</font><font color="#0000ff" size=2>=</font><font size=2>&quot;</font><font color="#0000ff" size=2>OnThreadPoolButtonClicked</font><font size=2>&quot;</font><font color="#0000ff" size=2> </font><font color="#ff0000" size=2>Margin</font><font color="#0000ff" size=2>=</font><font size=2>&quot;</font><font color="#0000ff" size=2>0,17,115,0</font><font size=2>&quot;</font><font color="#0000ff" size=2> </font><font color="#ff0000" size=2>Name</font><font color="#0000ff" size=2>=</font><font size=2>&quot;</font><font color="#0000ff" size=2>button2</font><font size=2>&quot;</font><font color="#0000ff" size=2> </font><font color="#ff0000" size=2>VerticalAlignment</font><font color="#0000ff" size=2>=</font><font size=2>&quot;</font><font color="#0000ff" size=2>Top</font><font size=2>&quot;</font><font color="#0000ff" size=2> </font><font color="#ff0000" size=2>HorizontalAlignment</font><font color="#0000ff" size=2>=</font><font size=2>&quot;</font><font color="#0000ff" size=2>Right</font><font size=2>&quot;</font><font color="#0000ff" size=2> </font><font color="#ff0000" size=2>Width</font><font color="#0000ff" size=2>=</font><font size=2>&quot;</font><font color="#0000ff" size=2>75</font><font size=2>&quot;</font><font color="#0000ff" size=2>&gt;</font><font size=2>ThreadPool</font><font color="#0000ff" size=2>&lt;/</font><font color="#800000" size=2>Button</font><font color="#0000ff" size=2>&gt;</p> <p>&lt;</font><font color="#800000" size=2>Button</font><font color="#0000ff" size=2> </font><font color="#ff0000" size=2>Height</font><font color="#0000ff" size=2>=</font><font size=2>&quot;</font><font color="#0000ff" size=2>23</font><font size=2>&quot;</font><font color="#0000ff" size=2> </font><font color="#ff0000" size=2>Click</font><font color="#0000ff" size=2>=</font><font size=2>&quot;</font><font color="#0000ff" size=2>OnUIThreadButtonClicked</font><font size=2>&quot;</font><font color="#0000ff" size=2> </font><font color="#ff0000" size=2>HorizontalAlignment</font><font color="#0000ff" size=2>=</font><font size=2>&quot;</font><font color="#0000ff" size=2>Right</font><font size=2>&quot;</font><font color="#0000ff" size=2> </font><font color="#ff0000" size=2>Margin</font><font color="#0000ff" size=2>=</font><font size=2>&quot;</font><font color="#0000ff" size=2>0,17,22,0</font><font size=2>&quot;</font><font color="#0000ff" size=2> </font><font color="#ff0000" size=2>Name</font><font color="#0000ff" size=2>=</font><font size=2>&quot;</font><font color="#0000ff" size=2>button3</font><font size=2>&quot;</font><font color="#0000ff" size=2> </font><font color="#ff0000" size=2>VerticalAlignment</font><font color="#0000ff" size=2>=</font><font size=2>&quot;</font><font color="#0000ff" size=2>Top</font><font size=2>&quot;</font><font color="#0000ff" size=2> </font><font color="#ff0000" size=2>Width</font><font color="#0000ff" size=2>=</font><font size=2>&quot;</font><font color="#0000ff" size=2>75</font><font size=2>&quot;</font><font color="#0000ff" size=2>&gt;</font><font size=2>UI Thread</font><font color="#0000ff" size=2>&lt;/</font><font color="#800000" size=2>Button</font><font color="#0000ff" size=2>&gt;</p> <p>&lt;/</font><font color="#800000" size=2>Grid</font><font color="#0000ff" size=2>&gt;</p> <p>&lt;/</font><font color="#800000" size=2>Window</font><font color="#0000ff" size=2>&gt;</p></font> <p> </p> <p><font face="Times New Roman, Times, Serif" size=3>Thanks,</font></p> <p><font face="Times New Roman, Times, Serif" size=3>Jonas</font></p></font>© 2009 Microsoft Corporation. All rights reserved.Sat, 25 Oct 2008 03:11:47 Zafc1fdbd-0cad-4bd2-83a1-b68069c99720http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/afc1fdbd-0cad-4bd2-83a1-b68069c99720#afc1fdbd-0cad-4bd2-83a1-b68069c99720http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/afc1fdbd-0cad-4bd2-83a1-b68069c99720#afc1fdbd-0cad-4bd2-83a1-b68069c99720Jonas76http://social.msdn.microsoft.com/Profile/en-US/?user=Jonas76Catch DispatcherUnhandledException from other Thread<p class=MsoNormal style="margin:0cm 0cm 0pt"><font face="Times New Roman, Times, Serif">Hi,</font></p> <p class=MsoNormal style="margin:0cm 0cm 0pt"><font face="Times New Roman, Times, Serif"> </font></p> <p class=MsoNormal style="margin:0cm 0cm 0pt"><font face="Times New Roman, Times, Serif">I’m having trouble to catch the DispatcherUnhandledException from other threads than the 'main thread'. If you read the MSDN documentation<span style="">  </span>about the </font><a title="http://msdn2.microsoft.com/en-us/library/system.windows.application.dispatcherunhandledexception.aspx" href="http://msdn2.microsoft.com/en-us/library/system.windows.application.dispatcherunhandledexception.aspx"><font face="Times New Roman, Times, Serif">DispatcherUnhandledException</font></a><font face="Times New Roman, Times, Serif"> it says:</font></p> <p class=MsoNormal style="margin:0cm 0cm 0pt"><font face="Times New Roman, Times, Serif"> </font></p> <p class=MsoNormal style="margin:0cm 0cm 0pt"><font face="Times New Roman, Times, Serif"> </font></p> <p class=MsoNormal style="margin:0cm 0cm 0pt"><font face="Times New Roman, Times, Serif"><b>DispatcherUnhandledException</b> is raised by an Application for each exception that is not handled within the AppDomain that the <b>Application</b> object was created in.</font></p> <p class=MsoNormal style="margin:0cm 0cm 0pt"><font face="Times New Roman, Times, Serif"> </font></p> <p class=MsoNormal style="margin:0cm 0cm 0pt"><font face="Times New Roman, Times, Serif">The <b>DispatcherUnhandledException</b> event handler is passed a DispatcherUnhandledExceptionEventArgs argument that contains contextual information regarding the exception, including:</font></p> <ul style="margin-top:0cm" type=disc> <li class=MsoNormal style="margin:0cm 0cm 0pt;tab-stops:list 36.0pt"><font face="Times New Roman, Times, Serif">The exception (Exception).</font></li> <li class=MsoNormal style="margin:0cm 0cm 0pt;tab-stops:list 36.0pt"><font face="Times New Roman, Times, Serif">The Dispatcher from which it originated (Dispatcher).</font></li></ul> <p class=MsoNormal style="margin:1.35pt 0cm"><span style="font-size:8.5pt;color:black;font-family:Verdana"><font face="Times New Roman, Times, Serif"></font></span></p> <p class=MsoNormal style="margin:0cm 0cm 0pt"><font face="Times New Roman, Times, Serif"> </font></p> <p class=MsoNormal style="margin:0cm 0cm 0pt"><font face="Times New Roman, Times, Serif">This means that the exception also should be called from other threads than the main thread.</font></p> <p class=MsoNormal style="margin:0cm 0cm 0pt"><font face="Times New Roman"></font> </p> <p class=MsoNormal style="margin:0cm 0cm 0pt"><font face="Times New Roman">If this isn't working, what's then the best way to handle global exceptions in WPF applications?</font></p> <p class=MsoNormal style="margin:0cm 0cm 0pt"><font face="Times New Roman, Times, Serif"> </font></p> <p class=MsoNormal style="margin:0cm 0cm 0pt"><font face="Times New Roman, Times, Serif">Note, it works fine to catch the </font><a title="http://msdn2.microsoft.com/en-us/library/system.appdomain.unhandledexception.aspx" href="http://msdn2.microsoft.com/en-us/library/system.appdomain.unhandledexception.aspx"><font face="Times New Roman, Times, Serif">AppDomain.UnhandledException</font></a><font face="Times New Roman, Times, Serif">. But then its to late to handle the exception and the application is shut down.</font></p> <p class=MsoNormal style="margin:0cm 0cm 0pt"><font face="Times New Roman, Times, Serif"> </font></p> <p class=MsoNormal style="margin:0cm 0cm 0pt"><font face="Times New Roman, Times, Serif"> </font></p> <p class=MsoNormal style="margin:0cm 0cm 0pt"><font face="Times New Roman, Times, Serif">An example, raising exception from ThreadPool and from main the thread: </font></p><font color="#0000ff" size=2> <p>using</font><font size=2> System;</font></p> <p><font color="#0000ff" size=2>using</font><font size=2> System.Windows;</p></font><font color="#0000ff" size=2> <p>using</font><font size=2> System.Collections.Generic;</p></font><font color="#0000ff" size=2> <p>using</font><font size=2> System.Text;</p></font><font color="#0000ff" size=2> <p>using</font><font size=2> System.Windows.Threading;</p></font><font color="#0000ff" size=2> <p>using</font><font size=2> System.Threading;</p></font><font color="#0000ff" size=2> <p>namespace</font><font size=2> DispatcherUnhandledExceptionApplication</p> <p>{</p> <p></font><font color="#0000ff" size=2>public</font><font size=2> </font><font color="#0000ff" size=2>class</font><font size=2> </font><font color="#008080" size=2>MyApplication</font><font size=2> : </font><font color="#008080" size=2>Application</p></font><font size=2> <p>{</p> <p>[</font><font color="#008080" size=2>STAThread</font><font size=2>]</p> <p></font><font color="#0000ff" size=2>public</font><font size=2> </font><font color="#0000ff" size=2>static</font><font size=2> </font><font color="#0000ff" size=2>void</font><font size=2> Main()</p> <p>{</p> <p></font><font color="#008080" size=2>MyApplication</font><font size=2> app = </font><font color="#0000ff" size=2>new</font><font size=2> </font><font color="#008080" size=2>MyApplication</font><font size=2>();</p> <p>app.InitializeComponent();</p> <p>app.Run(); </p> <p>}</p> <p></font><font color="#0000ff" size=2>protected</font><font size=2> </font><font color="#0000ff" size=2>override</font><font size=2> </font><font color="#0000ff" size=2>void</font><font size=2> OnStartup(</font><font color="#008080" size=2>StartupEventArgs</font><font size=2> e)</p> <p>{</p> <p></font><font color="#0000ff" size=2>base</font><font size=2>.OnStartup(e);</p> <p></font><font color="#008080" size=2>Window</font><font size=2> aWindow = </font><font color="#0000ff" size=2>new</font><font size=2> </font><font color="#008080" size=2>Window</font><font size=2>();</p> <p>aWindow.ShowDialog();</p> <p>} </p> <p></font><font color="#0000ff" size=2>public</font><font size=2> </font><font color="#0000ff" size=2>void</font><font size=2> InitializeComponent()</p> <p>{</p> <p></font><font color="#008080" size=2>AppDomain</font><font size=2>.CurrentDomain.UnhandledException += </font><font color="#0000ff" size=2>new</font><font size=2> </font><font color="#008080" size=2>UnhandledExceptionEventHandler</font><font size=2>(CurrentDomain_UnhandledException);</p> <p>DispatcherUnhandledException += </font><font color="#0000ff" size=2>new</font><font size=2> System.Windows.Threading.</font><font color="#008080" size=2>DispatcherUnhandledExceptionEventHandler</font><font size=2>(App_DispatcherUnhandledException);</p> <p>}</p> <p></p> <p></font><font color="#0000ff" size=2>void</font><font size=2> CurrentDomain_UnhandledException(</font><font color="#0000ff" size=2>object</font><font size=2> sender, </font><font color="#008080" size=2>UnhandledExceptionEventArgs</font><font size=2> e)</p> <p>{</p> <p>}</p> <p></p> <p></font><font color="#0000ff" size=2>void</font><font size=2> App_DispatcherUnhandledException(</font><font color="#0000ff" size=2>object</font><font size=2> sender, System.Windows.Threading.</font><font color="#008080" size=2>DispatcherUnhandledExceptionEventArgs</font><font size=2> e)</p> <p>{</p> <p>e.Handled = </font><font color="#0000ff" size=2>true</font><font size=2>;</p> <p>System.</font><font color="#008080" size=2>Console</font><font size=2>.WriteLine(</font><font color="#800000" size=2>&quot;DispatcherUnhandledException captured!&quot;</font><font size=2>);</p> <p>}</p> <p>}</p> <p>}</p> <p> </p> <p> </p> <p> </p><font color="#0000ff" size=2> <p>using</font><font size=2> System;</p></font><font color="#0000ff" size=2> <p>using</font><font size=2> System.Collections.Generic;</p></font><font color="#0000ff" size=2> <p>using</font><font size=2> System.Text;</p></font><font color="#0000ff" size=2> <p>using</font><font size=2> System.Windows;</p></font><font color="#0000ff" size=2> <p>using</font><font size=2> System.Windows.Controls;</p></font><font color="#0000ff" size=2> <p>using</font><font size=2> System.Windows.Data;</p></font><font color="#0000ff" size=2> <p>using</font><font size=2> System.Windows.Documents;</p></font><font color="#0000ff" size=2> <p>using</font><font size=2> System.Windows.Input;</p></font><font color="#0000ff" size=2> <p>using</font><font size=2> System.Windows.Media;</p></font><font color="#0000ff" size=2> <p>using</font><font size=2> System.Windows.Media.Imaging;</p></font><font color="#0000ff" size=2> <p>using</font><font size=2> System.Windows.Shapes;</p></font><font color="#0000ff" size=2> <p>using</font><font size=2> System.Threading;</p></font><font color="#0000ff" size=2> <p>using</font><font size=2> System.Windows.Threading;</p> <p> </p></font><font color="#0000ff" size=2> <p>namespace</font><font size=2> DispatcherUnhandledExceptionApplication</p> <p>{</p> <p></font><font color="#808080" size=2>///</font><font color="#008000" size=2> </font><font color="#808080" size=2>&lt;summary&gt;</p></font><font size=2> <p></font><font color="#808080" size=2>///</font><font color="#008000" size=2> Interaction logic for Window1.xaml</p></font><font size=2> <p></font><font color="#808080" size=2>///</font><font color="#008000" size=2> </font><font color="#808080" size=2>&lt;/summary&gt;</p></font><font size=2> <p></font><font color="#0000ff" size=2>public</font><font size=2> </font><font color="#0000ff" size=2>partial</font><font size=2> </font><font color="#0000ff" size=2>class</font><font size=2> </font><font color="#008080" size=2>Window</font><font size=2> : System.Windows.</font><font color="#008080" size=2>Window</p></font><font size=2> <p>{</p> <p></font><font color="#0000ff" size=2>public</font><font size=2> Window()</p> <p>{</p> <p>InitializeComponent();</p> <p>}</p> <p></font><font color="#0000ff" size=2>void</font><font size=2> OnUIThreadButtonClicked(</font><font color="#0000ff" size=2>object</font><font size=2> sender, </font><font color="#008080" size=2>EventArgs</font><font size=2> e)</p> <p>{</p> <p>RaiseArgumentNullException();</p> <p>}</p> <p></font><font color="#0000ff" size=2>void</font><font size=2> OnThreadPoolButtonClicked(</font><font color="#0000ff" size=2>object</font><font size=2> sender, </font><font color="#008080" size=2>EventArgs</font><font size=2> e)</p> <p>{</p> <p></font><font color="#008080" size=2>ThreadPool</font><font size=2>.QueueUserWorkItem(</font><font color="#0000ff" size=2>new</font><font size=2> </font><font color="#008080" size=2>WaitCallback</font><font size=2>(</font><font color="#0000ff" size=2>delegate</font><font size=2>(</font><font color="#0000ff" size=2>object</font><font size=2> state)</p> <p>{</p> <p></font><font color="#0000ff" size=2>try</p></font><font size=2> <p>{</p> <p></font><font color="#008080" size=2>Dispatcher</font><font size=2>.CurrentDispatcher.UnhandledException += </font><font color="#0000ff" size=2>new</font><font size=2> </font><font color="#008080" size=2>DispatcherUnhandledExceptionEventHandler</font><font size=2>(CurrentDispatcher_UnhandledException);</p> <p></font><font color="#0000ff" size=2>int</font><font size=2> aDomainID = </font><font color="#008080" size=2>AppDomain</font><font size=2>.CurrentDomain.Id;</p> <p></font><font color="#008080" size=2>Thread</font><font size=2>.CurrentThread.Name = </font><font color="#800000" size=2>&quot;MyThreadPoolThread&quot;</font><font size=2>;</p> <p></font><font color="#008080" size=2>Dispatcher</font><font size=2>.CurrentDispatcher.Invoke(</font><font color="#008080" size=2>DispatcherPriority</font><font size=2>.Normal, </font><font color="#0000ff" size=2>new</font><font size=2> </font><font color="#008080" size=2>MyDlg</font><font size=2>(RaiseArgumentNullException));</p> <p>}</p> <p></font><font color="#0000ff" size=2>catch</font><font size=2> (</font><font color="#008080" size=2>Exception</font><font size=2> ex)</p> <p>{</p> <p></font><font color="#0000ff" size=2>throw</font><font size=2> ex;</p> <p>}</p> <p></font><font color="#0000ff" size=2>finally</p></font><font size=2> <p>{</p> <p></font><font color="#008080" size=2>Dispatcher</font><font size=2>.CurrentDispatcher.UnhandledException -= </font><font color="#0000ff" size=2>new</font><font size=2> </font><font color="#008080" size=2>DispatcherUnhandledExceptionEventHandler</font><font size=2>(CurrentDispatcher_UnhandledException);</p> <p>}</p> <p>}));</p> <p>}</p> <p></font><font color="#0000ff" size=2>void</font><font size=2> CurrentDispatcher_UnhandledException(</font><font color="#0000ff" size=2>object</font><font size=2> sender, </font><font color="#008080" size=2>DispatcherUnhandledExceptionEventArgs</font><font size=2> e)</p> <p>{</p> <p>e.Handled = </font><font color="#0000ff" size=2>false</font><font size=2>;</p> <p>}</p> <p></font><font color="#0000ff" size=2>delegate</font><font size=2> </font><font color="#0000ff" size=2>void</font><font size=2> </font><font color="#008080" size=2>MyDlg</font><font size=2>();</p> <p></font><font color="#0000ff" size=2>void</font><font size=2> RaiseArgumentNullException()</p> <p>{</p> <p></font><font color="#0000ff" size=2>throw</font><font size=2> </font><font color="#0000ff" size=2>new</font><font size=2> </font><font color="#008080" size=2>ArgumentNullException</font><font size=2>();</p> <p>} </p></font><font size=2> <p>}</p> <p>}</p></font> <p> </p> <p> </p> <p> </p> <p> </p> <p> </p><font color="#0000ff" size=2> <p>&lt;</font><font color="#800000" size=2>Window</font><font color="#0000ff" size=2> </font><font color="#ff0000" size=2>x:Class</font><font color="#0000ff" size=2>=</font><font size=2>&quot;</font><font color="#0000ff" size=2>DispatcherUnhandledExceptionApplication.Window</font><font size=2>&quot;</p></font><font color="#0000ff" size=2> <p></font><font color="#ff0000" size=2>xmlns</font><font color="#0000ff" size=2>=</font><font size=2>&quot;</font><font color="#0000ff" size=2>http://schemas.microsoft.com/winfx/2006/xaml/presentation</font><font size=2>&quot;</p></font><font color="#0000ff" size=2> <p></font><font color="#ff0000" size=2>xmlns:x</font><font color="#0000ff" size=2>=</font><font size=2>&quot;</font><font color="#0000ff" size=2>http://schemas.microsoft.com/winfx/2006/xaml</font><font size=2>&quot;</p></font><font color="#0000ff" size=2> <p></font><font color="#ff0000" size=2>Title</font><font color="#0000ff" size=2>=</font><font size=2>&quot;</font><font color="#0000ff" size=2>WindowsApplication17</font><font size=2>&quot;</font><font color="#0000ff" size=2> </font><font color="#ff0000" size=2>Height</font><font color="#0000ff" size=2>=</font><font size=2>&quot;</font><font color="#0000ff" size=2>97</font><font size=2>&quot;</font><font color="#0000ff" size=2> </font><font color="#ff0000" size=2>Width</font><font color="#0000ff" size=2>=</font><font size=2>&quot;</font><font color="#0000ff" size=2>300</font><font size=2>&quot;</p></font><font color="#0000ff" size=2> <p>&gt;</p> <p>&lt;</font><font color="#800000" size=2>Grid</font><font color="#0000ff" size=2>&gt;</p></font><font color="#0000ff" size=2> <p>&lt;</font><font color="#800000" size=2>Button</font><font color="#0000ff" size=2> </font><font color="#ff0000" size=2>Height</font><font color="#0000ff" size=2>=</font><font size=2>&quot;</font><font color="#0000ff" size=2>23</font><font size=2>&quot;</font><font color="#0000ff" size=2> </font><font color="#ff0000" size=2>Click</font><font color="#0000ff" size=2>=</font><font size=2>&quot;</font><font color="#0000ff" size=2>OnThreadPoolButtonClicked</font><font size=2>&quot;</font><font color="#0000ff" size=2> </font><font color="#ff0000" size=2>Margin</font><font color="#0000ff" size=2>=</font><font size=2>&quot;</font><font color="#0000ff" size=2>0,17,115,0</font><font size=2>&quot;</font><font color="#0000ff" size=2> </font><font color="#ff0000" size=2>Name</font><font color="#0000ff" size=2>=</font><font size=2>&quot;</font><font color="#0000ff" size=2>button2</font><font size=2>&quot;</font><font color="#0000ff" size=2> </font><font color="#ff0000" size=2>VerticalAlignment</font><font color="#0000ff" size=2>=</font><font size=2>&quot;</font><font color="#0000ff" size=2>Top</font><font size=2>&quot;</font><font color="#0000ff" size=2> </font><font color="#ff0000" size=2>HorizontalAlignment</font><font color="#0000ff" size=2>=</font><font size=2>&quot;</font><font color="#0000ff" size=2>Right</font><font size=2>&quot;</font><font color="#0000ff" size=2> </font><font color="#ff0000" size=2>Width</font><font color="#0000ff" size=2>=</font><font size=2>&quot;</font><font color="#0000ff" size=2>75</font><font size=2>&quot;</font><font color="#0000ff" size=2>&gt;</font><font size=2>ThreadPool</font><font color="#0000ff" size=2>&lt;/</font><font color="#800000" size=2>Button</font><font color="#0000ff" size=2>&gt;</p> <p>&lt;</font><font color="#800000" size=2>Button</font><font color="#0000ff" size=2> </font><font color="#ff0000" size=2>Height</font><font color="#0000ff" size=2>=</font><font size=2>&quot;</font><font color="#0000ff" size=2>23</font><font size=2>&quot;</font><font color="#0000ff" size=2> </font><font color="#ff0000" size=2>Click</font><font color="#0000ff" size=2>=</font><font size=2>&quot;</font><font color="#0000ff" size=2>OnUIThreadButtonClicked</font><font size=2>&quot;</font><font color="#0000ff" size=2> </font><font color="#ff0000" size=2>HorizontalAlignment</font><font color="#0000ff" size=2>=</font><font size=2>&quot;</font><font color="#0000ff" size=2>Right</font><font size=2>&quot;</font><font color="#0000ff" size=2> </font><font color="#ff0000" size=2>Margin</font><font color="#0000ff" size=2>=</font><font size=2>&quot;</font><font color="#0000ff" size=2>0,17,22,0</font><font size=2>&quot;</font><font color="#0000ff" size=2> </font><font color="#ff0000" size=2>Name</font><font color="#0000ff" size=2>=</font><font size=2>&quot;</font><font color="#0000ff" size=2>button3</font><font size=2>&quot;</font><font color="#0000ff" size=2> </font><font color="#ff0000" size=2>VerticalAlignment</font><font color="#0000ff" size=2>=</font><font size=2>&quot;</font><font color="#0000ff" size=2>Top</font><font size=2>&quot;</font><font color="#0000ff" size=2> </font><font color="#ff0000" size=2>Width</font><font color="#0000ff" size=2>=</font><font size=2>&quot;</font><font color="#0000ff" size=2>75</font><font size=2>&quot;</font><font color="#0000ff" size=2>&gt;</font><font size=2>UI Thread</font><font color="#0000ff" size=2>&lt;/</font><font color="#800000" size=2>Button</font><font color="#0000ff" size=2>&gt;</p> <p>&lt;/</font><font color="#800000" size=2>Grid</font><font color="#0000ff" size=2>&gt;</p> <p>&lt;/</font><font color="#800000" size=2>Window</font><font color="#0000ff" size=2>&gt;</p></font> <p> </p> <p><font face="Times New Roman, Times, Serif" size=3>Thanks,</font></p> <p><font face="Times New Roman, Times, Serif" size=3>Jonas</font></p></font>Mon, 29 Jan 2007 15:09:23 Z2007-02-09T00:29:09Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/afc1fdbd-0cad-4bd2-83a1-b68069c99720#a74b7d6f-6d50-4054-8163-6b0859999ceehttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/afc1fdbd-0cad-4bd2-83a1-b68069c99720#a74b7d6f-6d50-4054-8163-6b0859999ceeChango V. - MSFThttp://social.msdn.microsoft.com/Profile/en-US/?user=Chango%20V.%20-%20MSFTCatch DispatcherUnhandledException from other Thread<p>Jonas, the statement in the documentation is wrong. Application.Current.DispatcherUnhandledException is just shorthand for Application.Current.Dispatcher.UnhandledException, making access to the event thread-safe. </p> <p>If you want to have something like centralized exception handling, with the ability to swallow the exception, you could follow this pattern:</p> <ol> <li>Make the Dispatcher object of the main thread available to your worker thread. (You could either pass it as a parameter to the thread start method or just use Application.Current.Dispatcher.)</li> <li>Use a try-catch-finally in the worker thread. (In general, the catch block should immediately rethrow critical exceptions: OutOfMemoryException, SecurityException, SEHException, maybe also NullReferenceException.)</li> <li>The catch block calls mainDispatcher.Invoke(DispatcherPriority.Send, ExceptionCallback, exception).</li> <li>ExceptionCallback() throws the exception object passed to it. This happens on the main thread, so handlers added to Application.DispatcherUnhandledException will be invoked and can &quot;handle&quot; the exception.</li> <li>If your worker thread resumes execution, the exception was &quot;handled.&quot;</li></ol>Wed, 07 Feb 2007 17:54:43 Z2007-02-09T00:29:09Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/afc1fdbd-0cad-4bd2-83a1-b68069c99720#b982be08-f3d5-4e17-946f-2707f3645415http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/afc1fdbd-0cad-4bd2-83a1-b68069c99720#b982be08-f3d5-4e17-946f-2707f3645415Mike Weinhardthttp://social.msdn.microsoft.com/Profile/en-US/?user=Mike%20WeinhardtCatch DispatcherUnhandledException from other Thread<p>FYI - The Application.DispatcherUnhandledException doc is being updated to be more precise. Also, I am trying to put in a couple of related samples:</p> <p><font face=Verdana>1) How to pass an exception from a background UI thread (thread with a Dispatcher) to the main UI thread<br></font><font face=Verdana>2) How to pass an exception from a background worker thread (thread without a Dispatcher) to the main UI thread</font></p> <p><font face=Verdana>Thanks for the good question.</font></p>Mon, 12 Feb 2007 22:45:33 Z2007-02-12T22:45:33Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/afc1fdbd-0cad-4bd2-83a1-b68069c99720#da2a455d-99df-45ab-b31e-ad70ff9c724ahttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/afc1fdbd-0cad-4bd2-83a1-b68069c99720#da2a455d-99df-45ab-b31e-ad70ff9c724aWill.Rogershttp://social.msdn.microsoft.com/Profile/en-US/?user=Will.RogersCatch DispatcherUnhandledException from other Thread<div><font face="Verdana, Geneva, Arial, Sans-serif"><font size=2>Michael,</font></font></div> <div><font face="Verdana, Geneva, Arial, Sans-serif"><font size=2></font></font> </div> <div><font face="Verdana, Geneva, Arial, Sans-serif"><font size=2>I'm in the midst of designing an error handling framework for my company's first WPF applications. If you could provide the samples you mentioned above here or to me by email it would be a great help. I am particularly concerned with how to best handle the two scenarios you mention above.</font></font></div> <div><font face="Verdana, Geneva, Arial, Sans-serif"><font size=2></font></font> </div> <div><font face="Verdana, Geneva, Arial, Sans-serif"><font size=2>Also, in the sample WPF app I'm coding in VS 2005, I'm not seeing where to hook up the DispatcherUnhandledExceptionEventHandler I've written. The expamples I've seen place this in the App's Main method, but that method resides in the App.g.cs file and is not to be hand-edited by developers.</font></font></div> <div><font face="Verdana, Geneva, Arial, Sans-serif"><font size=2></font></font> </div> <div><font face="Verdana, Geneva, Arial, Sans-serif"><font size=2>Thanks in advance for any help you (or anyone reading this) can provide.</font></font></div> <div><font size=2>Will.</font><font face="Verdana, Geneva, Arial, Sans-serif"></div></font>Thu, 22 Feb 2007 20:54:24 Z2007-02-22T20:54:24Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/afc1fdbd-0cad-4bd2-83a1-b68069c99720#2b7b9c86-2811-48fb-b901-ccb1230b2d74http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/afc1fdbd-0cad-4bd2-83a1-b68069c99720#2b7b9c86-2811-48fb-b901-ccb1230b2d74Jim.Rogershttp://social.msdn.microsoft.com/Profile/en-US/?user=Jim.RogersCatch DispatcherUnhandledException from other Thread<p>For anyone stumbling across this thread, I've got a blog post about the implementation of this here:<br><a href="http://www.jimandkatrin.com/codeblog/2008/06/wpf-worker-thread-exceptions.html">http://www.jimandkatrin.com/codeblog/2008/06/wpf-worker-thread-exceptions.html</a></p><hr size="1" align="left" width="25%">negative ghostrider - the pattern is fullSat, 25 Oct 2008 03:11:47 Z2008-10-25T03:11:47Z