frenk_nick
frenk_nick

Reputation: 19

Error in Dispatcher.Invoke

Yes, i'm sorry. So, i've a new WPF project, that will show once it starts to stream an IP camera. To do this I use the string that shows me only an image. However, to ensure that it becomes video, I use the thread. Because the string for the video streaming doesn't work. To avoid problems of conflict of access of different threads, use the delegates. I post the code of the delegate and the delegate method:

 public delegate void Del(Image images, string url);

        public void setImage(Image images,string url)
        {
            if (images.Dispatcher.CheckAccess())
            {
                Del ert = new Del(setImage);
                images.Dispatcher.Invoke(ert, new object[] { images, url });
                BitmapImage img = new BitmapImage();
                img.BeginInit();
                img.UriSource = new Uri(url);
                img.EndInit(); // Getting exception here 
                images.Source = img;
            }
            else
            {
                images.Dispatcher.BeginInvoke(new Del(setImage), DispatcherPriority.Normal,
                                                     new object[] { images, url });

            }
        }


        public void StreamImg()
        {
            while (true)
            {
                var date = DateTime.Today.Hour;
                setImage(image1, @"http://ipaddress/jpg/image.jpg" + "?" + date);
                Thread.Sleep(10);
             }
        }

But i've error in:

images.Dispatcher.Invoke(ert, new object[] { images, url });

the error is

An unhandled exception of type 'System.StackOverflowException' in WindowsBase.dll

I hope I was more clear, I'm sorry but I'm new to the forum

Upvotes: 0

Views: 1277

Answers (1)

Russ
Russ

Reputation: 4163

Your code recursively calls itself forever, causing the stackoverflow.

The setImage method is called...

The images.Dispatcher.CheckAccess method either returns true or false.

if true, you invoke the dispatcher to call setImage again.

if false you begininvoke the dispatcher to call setImage again.

And the whole process repeats, and can never return from the invoke, because it just keeps calling setImage deeper and deeper until the stack overflows.

Taking the dispatcher.invoke out of the area where CheckAccess returns true will probably fix your problem.

Upvotes: 2

Related Questions