c# - WPF 中图像更新的 TargetInvocationException

标签 c# wpf exception targetinvocationexception

我构建了一个显示图像的 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,我做错了什么?

最佳答案

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

您是否试过像这样直接调用调度程序上的操作:

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

关于c# - WPF 中图像更新的 TargetInvocationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2120719/

相关文章:

c# - Wpf在Grid内绘制对象集合

wpf - 使用 WrapPanel 样式化 ListView.GroupStyle

java - 抛出异常时触发的事件

c# - 在 SQL Server 中将二进制文件保存为 BLOB 和文本(或从全文索引中获取文本)

c# - 在非 IE 浏览器中禁用 ASP.NET ImageButton

c# - 当我点击其他地方时,我的 WPF ContextMenu 没有消失

java - 什么时候抛出/捕获一般异常是可以接受的

android - 从错误的线程异常调用

c# - FirebirdSql (C#) 比在 FlameRobin 中执行脚本慢

javascript - 如何从在客户端添加行的表中获取数据?