Here's my hypothetical example. I have a very simple WPF window with a one Button. The Button.Click event has a handler that goes like this.
这是我的假设例子。我有一个非常简单的WPF窗口,带有一个Button。 Button.Click事件有一个像这样的处理程序。
Action doit = () =>
{
Action error = () => { throw new InvalidOperationException("test"); };
try {
this.Dispatcher.Invoke(error, DispatcherPriority.Normal);
} catch (Exception ex) {
System.Diagnostics.Trace.WriteLine(ex);
throw;
}
};
doit.BeginInvoke(null, null);
I would expect that the exception would be caught and written down by the Trace.WriteLine
call. Instead, no exception is caught and the application breaks.
我希望通过Trace.WriteLine调用捕获并写下异常。相反,没有捕获异常并且应用程序中断。
Does anybody knows of an possible explanation for this to happen? And what workaround do you suggest in order to catch exceptions thrown by the delegate invoked by Dispatcher.Invoke
?
有人知道可能的解释吗?为了捕获由Dispatcher.Invoke调用的委托抛出的异常,您建议使用哪种解决方法?
Update 1: I put a throw
in the exception handling code. I don't want to actually ignore the exception. The whole point of my question is to handle it correctly. The problem is that the exception handling code is never executed.
更新1:我抛出异常处理代码。我不想实际忽略该异常。我的问题的重点是正确处理它。问题是永远不会执行异常处理代码。
Remember that this is an hypothetical example. My real code does not look like that. Also, assume that I can't change the code in the method to be invoked.
请记住,这是一个假设的例子。我的真实代码看起来不像那样。另外,假设我无法更改要调用的方法中的代码。
Update 2: Consider this similar example. Instead of a WPF window, I have a Windows Forms window. It has a button with the almost exactly the same handler. The only difference is in the invocation code. It goes like this.
更新2:考虑这个类似的例子。我有一个Windows窗体,而不是WPF窗口。它有一个按钮,几乎完全相同的处理程序。唯一的区别在于调用代码。它是这样的。
this.Invoke(error);
In Windows Forms, the exception handling code is executed. Why the difference?
在Windows窗体中,执行异常处理代码。为什么不同?
1 个解决方案
#1
5
UPDATED: To observe the exception in the other thread, you want to use a Task
, queue it to the Dispatcher
thread (using TaskScheduler.FromCurrentSynchronizationContext
), and wait on it, as such:
更新:要观察另一个线程中的异常,您要使用Task,将其排入Dispatcher线程(使用TaskScheduler.FromCurrentSynchronizationContext),并等待它,如下所示:
var ui = TaskScheduler.FromCurrentSynchronizationContext();
Action doit = () =>
{
var error = Task.Factory.StartNew(
() => { throw new InvalidOperationException("test"); },
CancellationToken.None,
TaskCreationOptions.None,
ui);
try {
error.Wait();
} catch (Exception ex) {
System.Diagnostics.Trace.WriteLine(ex);
}
};
doit.BeginInvoke(null, null);
UPDATED (again): Since your goal is a reusable component, I do recommend moving to a Task
-based interface or something else based on SynchronizationContext
such as the event-based asynchronous pattern, instead of basing the component on Dispatcher
or ISynchronizeInvoke
.
更新(再次):由于您的目标是可重用的组件,我建议您转移到基于任务的界面或基于SynchronizationContext的其他内容,例如基于事件的异步模式,而不是基于Dispatcher或ISynchronizeInvoke的组件。
Dispatcher
-based components only work on WPF/Silverlight; ISynchronizeInvoke
-based components only work on Windows Forms. SynchronizationContext
-based components will work with WPF or Windows Forms transparently, and (with a bit more work) ASP.NET, console apps, windows services, etc.
基于调度程序的组件仅适用于WPF / Silverlight;基于ISynchronizeInvoke的组件仅适用于Windows窗体。基于SynchronizationContext的组件将透明地与WPF或Windows Forms一起使用,并且(有更多工作)ASP.NET,控制台应用程序,Windows服务等。
The event-based asynchronous pattern is the old recommended way of writing SynchronizationContext
-based components; it's still around for .NET 3.5-era code. If you're on .NET 4, though, the task parallel library is much more flexible, clean, and powerful. The TaskScheduler.FromCurrentSynchronizationContext
uses SynchronizationContext
underneath, and is the New Way to write reusable components that need this kind of synchronization.
基于事件的异步模式是编写基于SynchronizationContext的组件的旧推荐方法;它仍然适用于.NET 3.5时代的代码。但是,如果您使用的是.NET 4,则任务并行库更加灵活,干净且功能强大。 TaskScheduler.FromCurrentSynchronizationContext使用下面的SynchronizationContext,是编写需要这种同步的可重用组件的新方法。
#1
5
UPDATED: To observe the exception in the other thread, you want to use a Task
, queue it to the Dispatcher
thread (using TaskScheduler.FromCurrentSynchronizationContext
), and wait on it, as such:
更新:要观察另一个线程中的异常,您要使用Task,将其排入Dispatcher线程(使用TaskScheduler.FromCurrentSynchronizationContext),并等待它,如下所示:
var ui = TaskScheduler.FromCurrentSynchronizationContext();
Action doit = () =>
{
var error = Task.Factory.StartNew(
() => { throw new InvalidOperationException("test"); },
CancellationToken.None,
TaskCreationOptions.None,
ui);
try {
error.Wait();
} catch (Exception ex) {
System.Diagnostics.Trace.WriteLine(ex);
}
};
doit.BeginInvoke(null, null);
UPDATED (again): Since your goal is a reusable component, I do recommend moving to a Task
-based interface or something else based on SynchronizationContext
such as the event-based asynchronous pattern, instead of basing the component on Dispatcher
or ISynchronizeInvoke
.
更新(再次):由于您的目标是可重用的组件,我建议您转移到基于任务的界面或基于SynchronizationContext的其他内容,例如基于事件的异步模式,而不是基于Dispatcher或ISynchronizeInvoke的组件。
Dispatcher
-based components only work on WPF/Silverlight; ISynchronizeInvoke
-based components only work on Windows Forms. SynchronizationContext
-based components will work with WPF or Windows Forms transparently, and (with a bit more work) ASP.NET, console apps, windows services, etc.
基于调度程序的组件仅适用于WPF / Silverlight;基于ISynchronizeInvoke的组件仅适用于Windows窗体。基于SynchronizationContext的组件将透明地与WPF或Windows Forms一起使用,并且(有更多工作)ASP.NET,控制台应用程序,Windows服务等。
The event-based asynchronous pattern is the old recommended way of writing SynchronizationContext
-based components; it's still around for .NET 3.5-era code. If you're on .NET 4, though, the task parallel library is much more flexible, clean, and powerful. The TaskScheduler.FromCurrentSynchronizationContext
uses SynchronizationContext
underneath, and is the New Way to write reusable components that need this kind of synchronization.
基于事件的异步模式是编写基于SynchronizationContext的组件的旧推荐方法;它仍然适用于.NET 3.5时代的代码。但是,如果您使用的是.NET 4,则任务并行库更加灵活,干净且功能强大。 TaskScheduler.FromCurrentSynchronizationContext使用下面的SynchronizationContext,是编写需要这种同步的可重用组件的新方法。