从异步回调更新 UI 组件

发布于 2024-09-30 08:02:53 字数 507 浏览 3 评论 0原文

现在我了解了 Dispatcher 和 DispatcherTimer 及其优点。 但我一直认为异步 Web 服务/WCF 回调(已完成的事件处理程序)是由 UI 线程自动处理的。

但看看网上的一些参考资料,例如下面链接的参考资料,似乎情况并非如此。

所以奇怪的是,我没有使用 Dispatcher 来更新服务完成事件中的 UI(更新数据绑定的 ObservableCollections),但我从未收到过跨线程异常。

谁能解释为什么我没有看到这个异常,或者确认我最初的假设是否正确?

参考: http://www.silverlightshow.net /items/Tip-Asynchronous-Silverlight-Execute-on-the-UI-thread.aspx

Now I know about the Dispatcher and the DispatcherTimer and their benefits.
But I've always been under the impression that an async web-service/WCF callback (completed event handler) are automatically handled by the UI thread.

But looking at some references online such as the one linked below, it seems this is NOT the case.

So the strange thing is that i haven't been using the Dispatcher to update the UI (updating data-bound ObservableCollections) within service completed events, yet I've never received a cross-thread exceptions.

Can anybody explain why i havent seen this exception, or confirm if my original assumption is correct?

Reference:
http://www.silverlightshow.net/items/Tip-Asynchronous-Silverlight-Execute-on-the-UI-thread.aspx

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

涫野音 2024-10-07 08:02:53

调度程序所做的就是将消息放入普通的 Windows 消息队列中。如果您更新绑定到 UI 元素的元素,则不需要使用调度程序,因为更新模型时引发的 PropertyChanged 已经将消息放入 Windows 消息队列中,因此您不需要调用任何调度程序,否则您只需在窗口消息队列中进行两次往返即可。

What the dispatcher do is putting a message into the normal windows message queuse. If you update an element bound to an UI element you don't need to use a dispatcher because the PropertyChanged raised when you update your model already put a message into the windows message queue, so you don't need to call any dispatcher, otherwise you just do two round trips into the window message queue.

維他命╮ 2024-10-07 08:02:53

最简单的解释是,这取决于您检索数据的方式以及是否尝试更新 UI。例如,当直接使用 HttpWebRequest 时,始终需要将其封送回 UI 线程。但是,如果您使用 WebClient,那么这就为您完成了。 WCF 还将为您做一些编组工作。

“Silverlight 应用程序中的 WCF 代理使用启动 Web 服务调用的线程的 SynchronizationContext 来安排收到响应时异步事件处理程序的调用。”

http://tomasz.janczuk.org/2009/08 /improving-performance-of-concurrent-wcf.html

换句话说,WCF 会将调用编组回调用它的线程。因此,如果您从 UI 线程调用服务调用,那么它们将返回到 UI 线程。如果您在不同的线程上调用服务,那么您将需要自己进行编组。

希望有帮助。

The easiest explanation is it depends on how you are retrieving your data and whether you are trying to update the UI. For instance, when using HttpWebRequest directly it will always need to be marshaled back to the UI thread. However if you are using WebClient then that is done for you. WCF will also do some marshaling for you.

"WCF proxies in Silverlight applications use the SynchronizationContext of the thread from which the web service call is initiated to schedule the invocation of the async event handler when the response is received."

http://tomasz.janczuk.org/2009/08/improving-performance-of-concurrent-wcf.html

In other words, WCF will marshal the call back to the thread in which it was called from. So if you are invoking your service calls from the UI thread, then they will come back on the UI thread. If you are invoking your services on a different thread then you will need to do the marshaling yourself.

Hope that helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文