WPF中的图像更新的TargetInvocationException

时间:2010-01-22 21:37:32

标签: c# wpf exception targetinvocationexception

我已经构建了一个显示图像的WPF控件。现在我想以非常快的速度更改该图像。 我已经构建了一个ImageContainer类来保存图像,并且有一个ChangedEventHandler,可以在更改时更新控件中的Image。

执行的代码如下所示:

videoImageThread = new Thread(
            new ThreadStart(
              delegate()
              {
                  this.VideoCapture.Dispatcher.Invoke(
                    System.Windows.Threading.DispatcherPriority.Normal,
                    new Action(
                      delegate()
                      {

                          videoImage.Source = VideoImageContainer.Instance.VideoBitmapSourceImage;

                      }
                  ));
              }
          ));


private void Instance_VideoRefresh()
    {
        if (VideoImageContainer.Instance.VideoImage != null)
        {
            lock (videoImageSetLock)
            {
                videoImageThread.Start();
            }
        }
    }

此代码抛出System.Reflection.TargetInvocationException,我做错了什么?

2 个答案:

答案 0 :(得分:1)

在我看来,你正在调用一个线程来调用一个线程?!

您是否尝试过直接调度调度程序上的操作:

private void Instance_VideoRefresh()
{
    if (VideoImageContainer.Instance.VideoImage != null)
        this.VideoCapture.Dispatcher.Invoke(
                System.Windows.Threading.DispatcherPriority.Normal,
                new Action(
                  delegate()
                  {
                      videoImage.Source = VideoImageContainer.Instance.VideoBitmapSourceImage;
                  }
              ));
}

答案 1 :(得分:0)

您是否尝试过将videoImage.Source绑定到属性,并在Instance_VideoRefresh方法中更改该属性?

我之前尝试使用Image / List< ImageSource> / Timer组合,它运行良好。