wpf调用线程无法访问此对象,因为另一个线程拥有该对象
2015-09-13 21:01
639 查看
WPF:Dispatcher.Invoke 方法,只有在其上创建 Dispatcher 的线程才可以直接访问DispatcherObject。若要从不同于在其上创建 DispatcherObject 的线程的某个线程访问 DispatcherObject,请对与 DispatcherObject 关联的 Dispatcher 调用 Invoke 或 BeginInvoke。需要强制线程安全的 DispatcherObject 的子类可以通过对所有公共方法调用 VerifyAccess
来强制线程安全。这样可以保证调用线程是在其上创建 DispatcherObject 的线程。
代码:
this.lbl.Dispatcher.Invoke(new Action(()=>{ this.lbl.Text = "this is a test!!!"; }));
this.lbl.Dispatcher.Invoke(new Action(()=>{ this.lbl.Text = "this is a test!!!"; }));
Winfrom:Control.Invoke 方法 (Delegate),在拥有此控件的基础窗口句柄的线程上执行指定的委托。
代码:
this.lbl.Invoke(new Action(()=>{ this.lbl.Text = "this is a test!!!"; }));
关于委托的几种写法 它们是等效的
对比以上:可以把 ()=> 替换成 delegate
原文地址:
http://blog.csdn.net/cselmu9/article/details/8274556
来强制线程安全。这样可以保证调用线程是在其上创建 DispatcherObject 的线程。
代码:
this.lbl.Dispatcher.Invoke(new Action(()=>{ this.lbl.Text = "this is a test!!!"; }));
this.lbl.Dispatcher.Invoke(new Action(()=>{ this.lbl.Text = "this is a test!!!"; }));
Winfrom:Control.Invoke 方法 (Delegate),在拥有此控件的基础窗口句柄的线程上执行指定的委托。
代码:
this.lbl.Invoke(new Action(()=>{ this.lbl.Text = "this is a test!!!"; }));
关于委托的几种写法 它们是等效的
this.labNow.Dispatcher.Invoke(()=> { this.labNow.Content = DateTime.Now.ToLongDateString() + DateTime.Now.ToLongTimeString(); }); this.labNow.Dispatcher.Invoke(new Action(() => { this.labNow.Content = DateTime.Now.ToShortTimeString(); }) ); this.labNow.Dispatcher.Invoke(new Action(delegate { this.labNow.Content = DateTime.Now.ToShortTimeString(); }));
对比以上:可以把 ()=> 替换成 delegate
原文地址:
http://blog.csdn.net/cselmu9/article/details/8274556
相关文章推荐
- UVa 11882:Biggest Number(DFS+剪枝)
- c++11——右值引用
- C++达到String分类
- POJ 2305大数求余
- ssh scp 复制文件和文件夹
- 能承受为生,侮辱技术好吗?
- HTML表单介绍
- C语言漫谈(二)
- Arraylist与Hashset的判断元素相同的依据
- Java中HashMap遍历的两种方式
- 实验室聚会感悟
- HTML&CSS Learning Notes 2
- hdu 5438 Ponds dfs
- hdu 5443 The Water Problem
- 浅析extern “C”的作用
- 杭州电子科技大学acm--2017
- 移动端的click
- 拉普拉斯矩阵/映射/聚类
- JS基本语法
- IT男的工作现状