Silverlight 4 多线程

发布于 2024-09-01 02:56:00 字数 224 浏览 8 评论 0原文

我正在尝试大约每 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

度的依靠╰つ 2024-09-08 02:56:00

使用Dispatcher BeginInvoke。例如:-

 private void MyCallback(object sender, SomeArgsClass e)
 {
     // perhaps some extraction of a payload or something
     Deployment.Current.Dispatcher.BeginInvoke( () =>
     {
        // Code you need to run on the UI thread.
     });

     // Note code may or may not exit here before code above has completed.
     // So be careful with disposable types etc.
 }

Use the Dispatcher BeginInvoke. For example:-

 private void MyCallback(object sender, SomeArgsClass e)
 {
     // perhaps some extraction of a payload or something
     Deployment.Current.Dispatcher.BeginInvoke( () =>
     {
        // Code you need to run on the UI thread.
     });

     // Note code may or may not exit here before code above has completed.
     // So be careful with disposable types etc.
 }
甜是你 2024-09-08 02:56:00

您可以采取多种方法:

  • 从后台线程使用 Deployment.Current.Dispatcher,并执行 Deployment.Current.Dispatcher.CheckAccess() 调用它

  • 传递来自启动后台线程的 UI 组件的调度程序,并使用该句柄执行 CheckAccess() 调用

  • 这是我的首选选项:将委托(回调)传递给后台线程,当它有新数据时,它调用该委托,并且该委托位于UI 控件 - 然后它可以使用 UI 控件上可用的 Dispatcher

此类事情的模式是:

private void DoMyUIUpdate(List<object> updates)
{
    if (Deployment.Current.Dispatcher.CheckAccess())
    {
        //do my work, update the UI
    }
    else
        Deployment.Current.Dispatcher.BeginInvoke(new Action<List<object>>(DoMyUIUpdate), updates);
}

There are several approaches you can take:

  • use Deployment.Current.Dispatcher from the background thread, and do a Deployment.Current.Dispatcher.CheckAccess() call on it

  • pass 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:

private void DoMyUIUpdate(List<object> updates)
{
    if (Deployment.Current.Dispatcher.CheckAccess())
    {
        //do my work, update the UI
    }
    else
        Deployment.Current.Dispatcher.BeginInvoke(new Action<List<object>>(DoMyUIUpdate), updates);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文