Silverlight 4 多线程
我正在尝试大约每 1/2 秒用新数据更新一次 Silverlight 4 UI。我已经使用 net.tcp 绑定并从服务器发出回调来连接到 WCF 服务。为了确保我尽快从服务中获取数据,我在 Silverlight 应用程序内的后台工作人员上启动了代理。
我的问题是,如何从回调中获取结果并更新绑定到 datagird 的 ObservableCollection?我尝试了多种不同的方法,但不断遇到可怕的跨线程错误。
I'm trying to update my Silverlight 4 UI approx every 1/2 second with new data. I've hooked into a WCF service using net.tcp binding and issuing callbacks from the server. To make sure I get the data from the service as quickly as possible I've started up my proxy on a backround worker inside of my Silverlight App.
My question is, how do I get the results from the callback and update the ObservableCollection that is bound to a datagird? I've tried a number of different ways and keep getting the dreaded cross-thread error.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用
Dispatcher
BeginInvoke
。例如:-Use the
Dispatcher
BeginInvoke
. For example:-您可以采取多种方法:
从后台线程使用
Deployment.Current.Dispatcher
,并执行 Deployment.Current.Dispatcher.CheckAccess() 调用它传递来自启动后台线程的 UI 组件的调度程序,并使用该句柄执行 CheckAccess() 调用
这是我的首选选项:将委托(回调)传递给后台线程,当它有新数据时,它调用该委托,并且该委托位于UI 控件 - 然后它可以使用 UI 控件上可用的 Dispatcher
此类事情的模式是:
There are several approaches you can take:
use
Deployment.Current.Dispatcher
from the background thread, and do a Deployment.Current.Dispatcher.CheckAccess() call on itpass the dispatcher from the UI component that starts the background thread, and use that handle to perform the CheckAccess() call
this is my preferred option: pass a delegate (callback) to the background thread, when it has new data it calls that delegate, and that delegate lives within the UI control - it can then use the Dispatcher available on the UI control
The pattern for this sort of thing is: