WPF中更新图片时出现目标调用异常(TargetInvocationException)

3
我已经构建了一个显示图像的WPF控件。现在我想以非常快的速度更改该图像。 我构建了一个ImageContainer类来持有该图像,并具有ChangedEventHandler,当该图像更改时更新我控件中的图像。
执行的代码如下:
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异常,我做错了什么?


可能是因为你正在访问属于另一个线程的调度程序导致的吗? - Andres
但这不就是关键吗?我不能从除控件线程之外的线程更新控件,否则会出现另一个异常。这就是为什么我有这段代码,它由某个工作线程调用并链接到控件线程。 - Alexander Stolz
2
查看异常的InnerException属性以找到真正的原因。 - Hans Passant
2个回答

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;
                  }
              ));
}

0
你尝试过将videoImage.Source简单地绑定到一个属性上,然后在Instance_VideoRefresh方法中更改该属性吗?
我之前尝试过使用Image/List/Timer组合,效果还不错。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接