跨线程访问UI控件时的Lambda表达式
工作中经常会用到跨线程访问UI控件的情况,由于.net本身机制,是不允许在非UI线程访问UI控件的,实际上跨线程访问UI控件还是 将访问UI的操作交给UI线程来处理的,
利用Control.Invoke方法,将操作传递给UI线程,不推荐使用CheckForIllegalCrossThreadCalls = false;
Control.Invoke的签名
//
// 摘要:
// 在拥有此控件的基础窗口句柄的线程上执行指定的委托。
//
// 参数:
// method:
// 包含要在控件的线程上下文中调用的方法的委托。
//
// 返回结果:
// 正在被调用的委托的返回值,或者如果委托没有返回值,则为 null。
[TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
public object Invoke(Delegate method);
但是如果我们利用Lambda表达式的时候,编译会报错。
如:
this.Invoke(() => { this.panel1.Controls.Remove(ucLoading); });
编译报错:无法将Lambda表达式转为System.Delegate类型,因为他不是委托类型
The problem the user is seeing is that the Thread ctor accepts a specific delegate -- the ThreadStart delegate. The C# compiler will check and make sure your anonymous method matches the signature of the ThreadStart delegate and, if so, produces the proper code under-the-covers to create the ThreadStart delegate for you.
But Control.Invoke is typed as accepting a "Delegate". This means it can accept any delegate-derived type. The example above shows an anonymous method that has a void return type and takes no parameters. It's possible to have a number of delegate-derived types that match that signature (such as MethodInvoker and ThreadStart -- just as an example). Which specific delegate should the C# compiler use? There's no way for it to infer the exact delegate type so the compiler complains with an error.
也就是说,对于Thread 构造函数来说,由于接受的是一个ThreadStart委托,编译器便可以将匿名函数与ThreadStart委托类型匹配,最后能够正确编译。
而对于Control.Invoke()来说,任何的代理类型都是可接受的,也就是说ThreadStart和MethodInvoker都是可以接受的类型。这样编译器反而不知道应该用哪个代理去匹配匿名函数了,导致了编译错误的发生。
我们就得加个委托名了
this.Invoke(new Action(() => { this.panel1.Controls.Remove(ucLoading); }));
或者
this.Invoke(new MethodInvoker(() => { this.panel1.Controls.Remove(ucLoading); }));
跨线程访问UI控件时的Lambda表达式的更多相关文章
- 理解SynchronizationContext,如何在Winform里面跨线程访问UI控件
SynchronizationContext 类是一个基类,可提供不带同步的自由线程上下文. 此类实现的同步模型的目的是使公共语言运行库内部的异步/同步操作能够针对不同的异步模型采取正确的行为.此模型 ...
- WPF / Win Form:多线程去修改或访问UI线程数据的方法( winform 跨线程访问UI控件 )
WPF:谈谈各种多线程去修改或访问UI线程数据的方法http://www.cnblogs.com/mgen/archive/2012/03/10/2389509.html 子线程非法访问UI线程的数据 ...
- 实现 winform 异步跨线程访问UI控件
在开发winform时经常会用到多线程防止界面出现假死现象,比如当你单击某个按钮时,需要执行很多代码,但是在执行过程中想实时的将当前执行的情况报告给用户,类型进度条或文本什么的. 这个时候很显然,如果 ...
- 实现跨线程访问UI控件的3种方法
namespace 多线程 { public partial class Form2 : Form { public Form2() { InitializeComponent(); } privat ...
- Visual studio之C#跨线程调用UI控件
背景 当前串口通讯项目,多个线程需要同时利用richTextBoxMsg控件打印信息,直接调用会造成线程不安全,严重的时候会直接导致UI线程挂掉,因此本篇就跨线程调用UI控件做个记录. 正文 定义控件 ...
- (转).NET 4.5中使用Task.Run和Parallel.For()实现的C# Winform多线程任务及跨线程更新UI控件综合实例
http://2sharings.com/2014/net-4-5-task-run-parallel-for-winform-cross-multiple-threads-update-ui-dem ...
- C# Winform 跨线程更新UI控件常用方法汇总(多线程访问UI控件)
概述 C#Winform编程中,跨线程直接更新UI控件的做法是不正确的,会时常出现“线程间操作无效: 从不是创建控件的线程访问它”的异常.处理跨线程更新Winform UI控件常用的方法有4种:1. ...
- wpf(怎么跨线程访问wpf控件)
在编写代码时,我们经常会碰到一些子线程中处理完的信息,需要通知另一个线程(我这边处理完了,该你了). 但是当我们通知WPF的UI线程时需要用到Dispatcher. 首先我们需要想好在UI控件上需要显 ...
- C# Winform 跨线程更新UI控件常用方法总结(转)
出处:http://www.tuicool.com/articles/FNzURb 概述 C#Winform编程中,跨线程直接更新UI控件的做法是不正确的,会时常出现“线程间操作无效: 从不是创建控件 ...
随机推荐
- QT5:先导篇 数据类型
一.简介 二.字符串类(QString) 三.日期类(QData) 四.时间类(QTime) 五.顺序容器类 Qt的顺序容器类有QList QLinkedList QVector QStack QQ ...
- vs2008如何新建自己工程的环境变量(局部)和 Windows系统(全局). .
在vs2008的Project->Property设置里经常会看到类似$(IntDir).$(OutDir).$(ProjectName) 的预定义宏.以vc2008为例,有时候我们在引用别的库 ...
- css float属性详解
定义和用法 float 属性定义元素在哪个方向浮动.以往这个属性总应用于图像,使文本围绕在图像周围,不过在 CSS 中,任何元素都可以浮动.浮动元素会生成一个块级框,而不论它本身是何种元素.如果浮动非 ...
- mysql错误Error(1133): Can’t find any matching row in the use
执行插入用户语句没有问题,但是执行权限赋值的时候提示:1133 - Can't find any matching row in the user table; 解决办法:插入新的用户成功时,需要刷新 ...
- uva 1592 Database (STL)
题意: 给出n行m列共n*m个字符串,问有没有在不同行r1,r2,有不同列c1,c2相同.即(r1,c1) = (r2,c1);(r1,c2) = (r2,c2); 如 2 3 123,456,789 ...
- Thawte SSL Web Server 多域型SSL证书
Thawte SSL Web Server 多域型SSL证书,最多支持25个域名,需要验证域名所有权和申请单位信息,属于企业验证型SSL证书,提供40位/56位/128位,最高支持256位自适应加密. ...
- PHP读取mysql中的数据
<!DOCTYPE HTML> <html> <head> <title> PHP动态读取mysql中的数据 </title> <me ...
- JavaEE JDBC PreparedStatement
PreparedStatement @author ixenos PreparedStatement工作原理 注意:虽然mysql不支持PreparedStatement优化,但依然有预编译的实现! ...
- CALayer之 customizing timing of an animation
customizing timing of an animation Timing is an important part of animations, and with Core Animatio ...
- POJ 1061 青蛙的约会【扩欧】
题意: 两只青蛙在地球同一纬度不同位置x,y向同一方向跳,每只青蛙跳的长度不同m,n,纬线总长度l,问两只青蛙是否能相遇,跳几次才相遇. 分析: 可知,问题可转化为求(m−n)∗a≡(y−x)(mod ...