3

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?

Alexander Stolz
  • 7,454
  • 12
  • 57
  • 64
  • Could it be because you are accessing a dispatcher that belongs to another thread? – Andres Jan 22 '10 at 21:52
  • But thats the point isn't it? I can't update a control from a thread other than the controls thread, if I do that I get another exception. Thats why I have this code, it is called by some worker thread and links to the controls thread – Alexander Stolz Jan 22 '10 at 21:56
  • 2
    Look at the InnerException property of the exception to find the real reason. – Hans Passant Jan 22 '10 at 22:17

2 Answers2

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;
                  }
              ));
}
Markus Hütter
  • 7,796
  • 1
  • 36
  • 63
0

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.

micahtan
  • 18,530
  • 1
  • 38
  • 33