从异步回调更新UI组件 [英] Updating UI components from an async callback

查看:101
本文介绍了从异步回调更新UI组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在,我了解Dispatcher和DispatcherTimer及其好处。
但是我一直给人的印象是,异步Web服务/ WCF回调(完成的事件处理程序)由UI线程自动处理。

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.

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

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?

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

推荐答案

调度程序所做的就是将消息放入普通的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.

这篇关于从异步回调更新UI组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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