0

是的,我很抱歉。所以,我有一个新的 WPF 项目,它会在它开始流式传输 IP 摄像机时显示。为此,我使用仅显示图像的字符串。但是,为了确保它变成视频,我使用了线程。因为视频流的字符串不起作用。为避免不同线程访问冲突的问题,请使用委托。我发布了委托和委托方法的代码:

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

但我有错误:

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

错误是

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

我希望我更清楚,对不起,我是论坛的新手

4

1 回答 1

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.

于 2013-06-18T21:11:43.287 回答