TargetInvocationException when updating image in WPF

I created a WPF control that displays an image. Now I would like to change this image at a very high speed. I created an ImageContainer class that contains an image and has a ChangedEventHandler that updates the image in my control when it changes.

The executed code 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 exception, what am I doing wrong?

+3
source share
2 answers

It seems to me that you are calling a thread to call a thread ?!

You tried to invoke an action in the dispatcher directly like this:

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

videoImage.Source Instance_VideoRefresh?

Image/List <ImageSource> /Timer, .

0

Source: https://habr.com/ru/post/1729630/


All Articles