0

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

frenk_nick
  • 19
  • 7
  • What does the stack trace look like? Where does that code occur? (For example, if it's unconditionally in `setImage`, that's not going to help...) – Jon Skeet Jun 17 '13 at 08:30
  • Can you clear up the first paragraph a bit? It's one long sentence, try to split it into at least four whole sentences, it might get a lot more readable that way. I cannot figure out what you are trying to do. – nvoigt Jun 17 '13 at 08:32
  • I can't answer my own question, because my reputation is less than 10...as soon as I will post more information – frenk_nick Jun 17 '13 at 08:57
  • but you can edit you own questions... did you try memory profiler for example [Ants Memory profiler](http://www.red-gate.com/products/dotnet-development/ants-memory-profiler/) – makc Jun 17 '13 at 10:27
  • ah ok, i don't know... now i do it! :) – frenk_nick Jun 17 '13 at 12:55

1 Answers1

2

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.

Russ
  • 4,091
  • 21
  • 32