C# 跨线程更新 UI
Winforms 跨线程更新 UI
在 Winforms 中, 所有的控件都包含 InvokeRequired 属性, 如果我们要更新UI,通过它我们可以判断是否需要调用 [Begin]Invoke.
直接使用
delegate void SetTextCallback(string text);
public void SetText(string text)
{
if (InvokeRequired)
{
var d = new SetTextCallback(SetText);
this.textBox1.Invoke(d, new object[] { text });
}
else
{
this.textBox1.Text = text;
}
}
直接调用 SetText 即可。
使用扩展方法
public static class MyClass
{
public static void InvokeIfRequired(this ISynchronizeInvoke obj, MethodInvoker action)
{
if (obj.InvokeRequired)
{
var args = new object[0];
obj.Invoke(action, args);
}
else
{
action();
}
}
}
使用:
this.textBox1.InvokeIfRequired(() =>
{
// Do anything you want with the control here
this.textBox1.Text = "text";
});
WPF 跨线程更新 UI
在 WPF 中,与 WinForms 中 InvokeRequired 属性相对应的有: DispatcherObject.CheckAccess() 和 Dispatcher.CheckAccess().
// Uses the Dispatcher.CheckAccess method to determine if
// the calling thread has access to the thread the UI object is on.
private void TryToUpdateButtonCheckAccess(object uiObject)
{
Button theButton = uiObject as Button;
if (theButton != null)
{
// Checking if this thread has access to the object.
if (theButton.Dispatcher.CheckAccess())
{
// This thread has access so it can update the UI thread.
UpdateButtonUI(theButton);
}
else
{
// This thread does not have access to the UI thread.
// Place the update method on the Dispatcher of the UI thread.
theButton.Dispatcher.BeginInvoke(DispatcherPriority.Normal,
new UpdateUIDelegate(UpdateButtonUI), theButton);
}
}
}
然而, CheckAccess 和 VerifyAccess 被标记为永远不在智能提示在显示,是否在暗示我们应该直接使用 Dispatcher.Invoke() 呢?
//
// 摘要:
// 确定调用线程是否为与此 System.Windows.Threading.Dispatcher 关联的线程。
//
// 返回结果:
// 如果调用线程是与此 System.Windows.Threading.Dispatcher 关联的线程,则为 true;否则为 false。
[EditorBrowsable(EditorBrowsableState.Never)]
public bool CheckAccess();
相关链接:
Correct usage (or not-usage) of Dispatcher.CheckAccess()
C# 跨线程更新 UI的更多相关文章
- 简短总结一下C#里跨线程更新UI(转)
摘自: http://my.oschina.net/sdqxcxh/blog/53707 跨线程更新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#里跨线程更新UI
摘自: http://my.oschina.net/sdqxcxh/blog/53707 跨线程更新UI是写多线程程序尤其是通信类的程序经常遇到的问题,这里面主要的问题是冲突,比如数据线程想要更新UI ...
- C# Winform 跨线程更新UI控件常用方法汇总(多线程访问UI控件)
概述 C#Winform编程中,跨线程直接更新UI控件的做法是不正确的,会时常出现“线程间操作无效: 从不是创建控件的线程访问它”的异常.处理跨线程更新Winform UI控件常用的方法有4种:1. ...
- C# Winform 跨线程更新UI控件常用方法总结(转)
出处:http://www.tuicool.com/articles/FNzURb 概述 C#Winform编程中,跨线程直接更新UI控件的做法是不正确的,会时常出现“线程间操作无效: 从不是创建控件 ...
- Winform之跨线程更新UI
Winform之跨线程更新UI 使用`Invoke`或者`BeginInvoke`与UI线程交互示例 参考及源码 使用Invoke或者BeginInvoke与UI线程交互示例 private void ...
- C# WinForms跨线程更新 UI
与在Android中一样, 子线程中更新UI被认为是线程不安全的, 会抛出异常. 子线程返回UI线程中更新UI的一个方法为: 1, 捕获应用的UI线程的上下文; 2, 定义线程任务; 3, 定义线程任 ...
- C#利用委托跨线程更新UI数据
转:http://www.2cto.com/kf/201206/136587.html 在使用C#的过程中,难免会用到多线程,而用多线程之后,线程如何与界面交互则是一个非常头疼的问题.其实不仅仅是界面 ...
- C# 跨线程更新UI界面的适当的处理方式,友好的退出界面的机制探索
本文主要讲讲C#窗体的程序中一个经常遇到的情况,就是在退出窗体的时候的,发生了退出的异常. 工业软件技术交流群:群1:592132877(满) 群2:948305931 欢迎技术探讨 我们先 ...
随机推荐
- linux抓取usb包设置usbmon
- Nginx location模块整理
location模块 Nginx location location 指令的作用是根据用户请求的URI来执行不同的应用,URI就是根据用户请求到的网址URL进行匹配,匹配成功了进行相关的操作. loc ...
- 关于Http协议,一片就够了
转载:http://www.jianshu.com/p/80e25cb1d81a HTTP简介 HTTP协议是Hyper Text Transfer Protocol(超文本传输协议)的缩写,是用于从 ...
- union共同体
定义: union 共用体名{ 成员列表}: 与结构体不同的是,共用体的所有成员占用同一段内存,修改一个成员会影响其余成员.但是结构体的各个成员会占不同的内存. 结构体占用的内存大于等于所有成员占用的 ...
- BZOJ 2809: [Apio2012]dispatching(可并堆 左偏树板题)
这道题只要读懂题目一切好说. 给出nnn个点的一棵树,每一个点有一个费用vvv和一个领导力aaa,给出费用上限mmm.求下面这个式子的最大值ax∗∣S∣ ( S⊂x的子树, ∑iv[i]≤m )\la ...
- mac使用sublime text3打开当前文件夹的终端
打开sublime text3,同时按住shift+command+p打开扩展列表, 选择Package Control: Install Pageage,回车. 在输入框输入: terminal,回 ...
- codeforces402B
Trees in a Row CodeForces - 402B The Queen of England has n trees growing in a row in her garden. At ...
- 记一次newApiHadoopRdd查询数据不一致问题
现象: +----------+-------+--------+-----+-----+-----+----+----+------+---------+-------+--------+----- ...
- TCP 之 FIN_WAIT_2状态处理流程
概述 在主动关闭方发送了FIN之后,进入FIN_WAIT_1状态,在此状态收到了ACK,则进入FIN_WAIT_2状态,而FIN_WAIT_2后续要做的工作是等待接收对端发过来的FIN包,并且发送AC ...
- koa 基础(二十)nodejs 操作mongodb数据库 --- 新增数据
1.app.js /** * nodejs 操作mongodb数据库 * 1.安装 操作mongodb * cnpm install mongodb --save * 2.引入 mongodb 下面的 ...