[英]WPF Dispatcher throws TargetInvocationException
I have a problem with my debugger, when faulty code is executed in the UI Thread the debugger correctly points out the faulty line, same when this is executed inside a thread, but it behaves kind of weird when called inside a dispatcher : TargetInvocationException is thrown in the disassembly. 我的调试器有问题,当在UI线程中执行了错误的代码时,调试器正确地指出了错误的行,与在线程内执行时相同,但是在调度程序中调用时,它的行为有点怪异:抛出TargetInvocationException在拆卸中。
How could I have it displayed properly and avoid this annoying message? 如何使其正确显示并避免出现此烦人的消息?
Here is a simple example that illustrates the problem: 这是一个说明问题的简单示例:
private void Button_Click_1(object sender, RoutedEventArgs e)
{
//navigator.NavigatToMenuAccueil(true);
//Throws NullPointerException
/*String x = null;
String y = x.ToUpper();*/
Thread th = new Thread(DoWork);
th.Start();
}
private void DoWork()
{
//Throws NullPointerException
/*String x = null;
String y = x.ToUpper();*/
Thread.Sleep(1000);
Dispatcher.BeginInvoke(new Action(() =>
{
//Throws TargetInnvocationException
/*
String x = null;
String y = x.ToUpper();
*/
MyTextBlock.Text = "My New Text";
}));
}
TargetInvocationException
is the exception that is thrown by methods invoked by reflection ( according to MSDN ), and by using BeginInvoke
, you are telling the Dispatcher
to do that. TargetInvocationException
是反射( 根据MSDN )通过反射调用的方法引发的异常,并且通过使用BeginInvoke
,您正在告诉Dispatcher
这样做。
Any exception that is thrown inside the passed delegate is wrapped in a TargetInvocationException
. 在传递的委托中引发的任何异常都包装在
TargetInvocationException
。 You can't prevent the Dispatcher from wrapping the original exeption. 您不能阻止分派器包装原始样品。 You can still get at the original exception by accessing
InnerException
though. 您仍然可以通过访问
InnerException
获得原始异常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.