[英]Error in Dispatcher.Invoke
是的,對不起。 所以,我有一個新的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
我希望我更清楚,對不起,但我是論壇的新手
您的代碼以遞歸方式自動調用自身,從而導致堆棧溢出。
調用setImage方法......
images.Dispatcher.CheckAccess方法返回true或false。
如果為true,則調用調度程序再次調用setImage。
如果為false,則開始調用調度程序再次調用setImage。
並且整個過程重復,並且永遠不能從調用返回,因為它只是在堆棧溢出之前一直調用setImage越來越深。
將dispatcher.invoke從CheckAccess返回true的區域中取出可能會解決您的問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.