子线程更新UI画面的方法
2016-04-01 17:36
429 查看
文章一开始,会给出三个版本的代码,其中有一个版本的代码在执行时会出错。那么,大家能一眼看出是哪个版本的代码有问题吗?
版本一:
版本二:
版本三:
如果你已经清楚子线程更新UI画面的方法,那么一眼就能看出来是版本二有问题。因为版本二的代码尝试在子线程中直接更新主画面内容,这显然是不被允许的,应该将更新UI画面的工作交给主线程来完成,而主线程通过this.Dispatcher来访问。这里的版本三同样是没有问题的。
版本一:
Action ac = () => { this.Label_Note.Content = "麻辣隔壁"; }; this.Dispatcher.BeginInvoke(ac, null);
版本二:
Action ac = () => { this.Label_Note.Content = "麻辣隔壁"; }; ac.BeginInvoke(null, null);
版本三:
Action ac = () => { this.Dispatcher.BeginInvoke(new Action(() => { this.Label_Note.Content = Thread.CurrentThread.Name; }), null); }; ac.BeginInvoke(null, null);
如果你已经清楚子线程更新UI画面的方法,那么一眼就能看出来是版本二有问题。因为版本二的代码尝试在子线程中直接更新主画面内容,这显然是不被允许的,应该将更新UI画面的工作交给主线程来完成,而主线程通过this.Dispatcher来访问。这里的版本三同样是没有问题的。
相关文章推荐
- 使用IntelliJ IDEA开发SpringMVC网站(三)数据库配置
- web.xml里<filter-mapping>中的<dispatcher>作用
- WPF 如何在线程中打开子窗体
- Struts框架之转发和重定向
- struts2 跳转类型 result type=chain、dispatcher、redirect(redirect-action)_forever
- web.xml之filter使用详解
- servlet里面那些要人蒙圈的方法
- 从零开始创建一个简单的springMVC工程(包含一些异常的解决方法)
- drp——Web组件交互的转发、包含
- org.apache.struts2.dispatcher.FilterDispatcher作用
- WPF multi-thread - Dispatcher WPF多线程 - Dispatcher
- wpf探秘之B哥Dispatcher私房菜(1)
- wpf探秘之B哥Dispatcher私房菜(2)最终回
- struts2 跳转类型、 result type=chain、dispatcher、redirect(redirect-action)
- 创建SpringMVC前端配置文件DispatcherServlet总是ClassNotFoundException
- struts2 跳转类型 result type=chain、dispatcher、redirect
- SpringMVC DispatcherServlet初始化过程
- java servlet
- (4)核心分发器
- WPF Dispatcher防止跨线程调用需要注意的问题