对C#中的WPF GUI的多线程访问 [英] Multithreaded access to the WPF GUI in C#

查看:109
本文介绍了对C#中的WPF GUI的多线程访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个自定义的内部应用程序,该应用程序将访问其他内部系统,这些内部系统通过UDP广播其名称和IP地址.我正在尝试创建一个多线程对话框,该对话框每500毫秒轮询一次UDP消息,持续15秒,解析UDP消息,然后将检测到的系统的名称添加到对话框中的列表框,以实时更新它.我已经测试并完成了UDP扫描代码,唯一的问题是跨线程更新ListBox.每当我尝试访问ListBox的Items或ItemSource属性时,都会得到System.InvalidOperationException:调用线程无法访问此对象,因为另一个线程拥有它."

I'm trying to create a custom, in-house application that is going to access other internal systems which broadcast their names and IP addresses via UDP. I'm trying to create a multi-threaded dialog that polls for UDP messages every 500 ms for 15 seconds, parses the UDP messages and then adds the names of the detected systems to a ListBox in the dialog, updating it in real time. I've already got the UDP scanning code tested and done, the only problem is updating the ListBox across threads. Any time I try to access the ListBox's Items or ItemSource properties, I get a System.InvalidOperationException : "The calling thread cannot access this object because a different thread owns it."

相关的堆栈跟踪部分:

   at System.Windows.Threading.Dispatcher.VerifyAccess()
   at System.Windows.DependencyObject.SetValue(DependencyProperty dp, Object value)
   at System.Windows.Controls.ItemsControl.set_ItemsSource(IEnumerable value)

无论我是否使用ObservableCollection(我知道,与集合类型无关),HashSet或任何其他对象,都将发生这种情况.有人可以帮助我在不同线程之间访问GUI吗?

This occurs regardless of whether I'm using an ObservableCollection (I know, has nothing to do with the collection type), a HashSet or any other object. Can anybody help me with accessing the GUI across different threads ?

推荐答案

您不能从其他线程安全地访问gui.所有调用都必须通过对Invoke的调用来分派,以在主线程上执行.这是Windows困扰多年的遗留问题.

You can't access the gui safely from other threads. All calls have to be dispatched via a call to Invoke to be executed on the main thread. This is a legacy hang-up that Windows has been saddled with for years.

这是一段代码,应该可以让您入门... (在此处找到: http: //social.msdn.microsoft.com/forums/zh-CN/wpf/thread/360540eb-d756-4434-86f9-a3449f05eb55/)

Here's a snippet of code that should get you started... (found here: http://social.msdn.microsoft.com/forums/en-US/wpf/thread/360540eb-d756-4434-86f9-a3449f05eb55/ )

if(textbox.Dispatcher.CheckAccess())
{
   // The calling thread owns the dispatcher, and hence the UI element
   textbox.AppendText(...);
}
else
{
   // Invokation required
   textbox.Dispatcher.Invoke(DispatcherPriority.Normal, [delegate goes here]);
}

此处还有其他说明: http://channel9.msdn.com/forums/TechOff/251835-WPF-Invoke-and-Anonymous-delegates/

这篇关于对C#中的WPF GUI的多线程访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆