TargetInvocationException on Image update in WPF

百般思念 提交于 2019-12-10 21:51:19

问题


I've built a WPF Control which displays an Image. Now I would like to change that image at a very fast rate. I've build an ImageContainer class which holds the image and has a ChangedEventHandler which updates the Image in my control when changed.

The code which is executed looks like this:

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

This code throws a System.Reflection.TargetInvocationException, what am I doing wrong?


回答1:


seems to me like you are invoking a thread to invoke a thread ?!

have you tried invoking the action on the dispatcher directly like so:

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



回答2:


Have you tried simply binding videoImage.Source to a property, and changing that property in your Instance_VideoRefresh method?

I've tried it before with an Image/List<ImageSource>/Timer combination, and it works pretty well.



来源:https://stackoverflow.com/questions/2120719/targetinvocationexception-on-image-update-in-wpf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!