简体   繁体   English

WPF调度程序抛出TargetInvocationException

[英]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.

相关问题 反序列化JSON会引发TargetInvocationException - Deserializing JSON throws TargetInvocationException 事件和IPCServerChannel引发TargetInvocationException - Events and IPCServerChannel throws TargetInvocationException WPF中的图像更新的TargetInvocationException - TargetInvocationException on Image update in WPF WPF:从Dispatcher修改CollectionView仍然会抛出错误 - WPF: Modifying CollectionView from Dispatcher still throws errors WPF Dispatcher.Run在Win 7 64上抛出null异常 - WPF Dispatcher.Run throws null exception on Win 7 64 使用 WebClient DownloadStringAsync 下载时抛出 TargetInvocationException - Throws TargetInvocationException when downloading with WebClient DownloadStringAsync 为什么Dispatcher.BeginInvoke为ThreadStart解包TargetInvocationException而不为Action解包? - Why does Dispatcher.BeginInvoke unwrap TargetInvocationException for ThreadStart but not for Action? 从BackgroundWorker.DoWork返回引发TargetInvocationException - Return from BackgroundWorker.DoWork throws TargetInvocationException XMLSerializer正确序列化,但是在反序列化时抛出TargetInvocationException - XMLSerializer serializes correctly, but throws TargetInvocationException when deserialzing TargetInvocationException? - TargetInvocationException?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM