Winform软件,不要在线程里操作UI
对于Winform软件,不要在线程里操作UI,不要相信:StartForm.CheckForIllegalCrossThreadCalls = false;
于是,把所有的代码都改成主线程委托调用的方式
private delegate void SetTextHandle(string id, string value);
private void ThreadSetText(string id, string value)
{
this.Controls.Find(id, true)[0].Text = value;
}
private void SetText(string id, string value)
{
if (this.InvokeRequired)
{
this.Invoke(new SetTextHandle(ThreadSetText), new object[] { id, value });
}
else
{
ThreadSetText(id, value);
}
}
2
// the canonical form (C# consumer)
public delegate void ControlStringConsumer(Control control, string text); // defines a delegate type
public void SetText(Control control, string text) {
if (control.InvokeRequired) {
control.Invoke(new ControlStringConsumer(SetText), new object[]{control, text}); // invoking itself
} else {
control.Text=text; // the "functional part", executing only on the main thread
}
}
3
public static class ControlHelpers
{
public static void InvokeIfRequired<T>(this T control, Action<T> action) where T : ISynchronizeInvoke
{
if (control.InvokeRequired)
{
control.Invoke(new Action(() => action(control)), null);
}
else
{
action(control);
}
}
}
Use it like this:
private void UpdateSummary(string text)
{
summary.InvokeIfRequired(s => { s.Text = text });
}
3
theLabel.Invoke(new Action(() => theLabel.Text = "hello world from worker thread!"));
4
public static void InvokeIfRequired(this Control control, MethodInvoker action)
{
if (control.InvokeRequired) {
control.Invoke(action);
} else {
action();
}
}
调用:
richEditControl1.InvokeIfRequired(() =>
{
// Do anything you want with the control here
richEditControl1.RtfText = value;
RtfHelpers.AddMissingStyles(richEditControl1);
});
更新为:
public static void InvokeIfRequired(this ISynchronizeInvoke obj,
MethodInvoker action)
{
if (obj.InvokeRequired) {
var args = new object[0];
obj.Invoke(action, args);
} else {
action();
}
}
考虑仍出现异常时:
while (!control.Visible)
{
System.Threading.Thread.Sleep(50);
}
5
public static void InvokeIfRequired(this Control c, Action<Control> action)
{
if(c.InvokeRequired)
{
c.Invoke(new Action(() => action(c)));
}
else
{
action(c);
}
}
变更为:
public static void InvokeIfRequired<T>(this T c, Action<T> action)
where T : Control
调用方法:
object1.InvokeIfRequired(c => { c.Visible = true; });
Winform软件,不要在线程里操作UI的更多相关文章
- 为什么在非UI线程中操作UI的改变失不安全的
因为你如果允许在非UI线程更新操作UI的东西,那我再另一个非UI线程也可以更新这个Ui的东西 这样就会有冲突,比如你的线程刚好跑到修改UI这里,我的另一个UI也有可能跑到这里,所以这样导致线程不安全. ...
- android4.0以上访问网络不能在主线程中进行以及在线程中操作UI的解决方法
MONO 调用一个线程操作UI 然后报Only the original thread that created a view hierarchy can touch its views.错误 goo ...
- android UI 操作 不要在子线程中操作UI
不管是android ,还是 ios ,请不要在子线程中操作UI,有时有些崩溃,从报错上看不出什么原因,就有可能是子线程操作了UI:切记,切记! 请放在主线程例: activity.runOnUiTh ...
- Android中进程与线程及如何在子线程中操作UI线程
1. Android进程 一个应用程序被启动时,系统默认创建执行一个叫做"main"的线程.这个线程也是你的应用与界面工具包(android.widget和android.view ...
- iOS子线程操作UI问题检查
iOS开发中,因为大部分函数都不是线程安全的,所以UI子线程中操作UI是非常危险的事,但是有时候因为开发者经验不足,不知道子线程中不能UI,或者知道但是写代码的时候没注意,或者不知道那些函数操作UI了 ...
- Android 操作UI线程的一些方法
我们经常会在后台线程中去做一些耗时的操作,比如去网络取数据.但是当数据取回来,需要显示到页面上的时候,会遇到一些小麻烦,因为我们都知道,android的UI页面是不允许在其他线程直接操作的.下面总结4 ...
- android 不能在子线程中更新ui的讨论和分析
问题描写叙述 做过android开发基本都遇见过 ViewRootImpl$CalledFromWrongThreadException,上网一查,得到结果基本都是仅仅能在主线程中更改 ui.子线程要 ...
- 线程中更新ui方法汇总
一.为何写作此文 你是不是经常看到很多书籍中说:不能在子线程中操作ui,不然会报错.你是不是也遇到了如下的疑惑(见下面的代码): @Override protected void onCreate ...
- c#线程间操作UI-Invoke和BeginInvoke
利用Invoke和或BeginInvoke实现线程间操作UI的简单例子. /* 窗体包含一个button和一个richtextbox控件 * 注:必须在子线程中执行Invoke和或BeginInvok ...
随机推荐
- Gulp构建前端自动化项目
类似于Grunt,gulp是另一个同样功能很强大的前端项目自动化利器. 下面是项目的效果:
- 用python+selenium抓取豆瓣读书中最受关注图书并按评分排序
抓取豆瓣读书中的(http://book.douban.com/)最受关注图书,按照评分排序,并保存至txt文件中,需要抓取书籍的名称,作者,评分,体裁和一句话评 方法一: #coding=utf-8 ...
- gcc编译的四个阶段:预处理,编译,汇编,链接
1:gcc编译的四个阶段:预处理,编译,汇编,链接 #vi file.c #gcc -E file.c -o file.i//-E查看且预处理后停止编译,-o生成目标文件,-i表示已预处理 #gcc ...
- 使用 OAuth2-Server-php 在 Yii 框架上搭建 OAuth2 Server
原文转自 http://www.cnblogs.com/ldms/p/4565547.html Yii 有很多 extension 可以使用,在查看了 Yii 官网上提供的与 OAuth 相关的扩展后 ...
- 前端构建工具gulp入门教程(share)
参考自:http://segmentfault.com/a/1190000000372547 资源: npm上得gulp组件 gulp的Github主页 官方package.json文档 gulp中文 ...
- .stop()
一. 在使用animate()的时候 前面需要加上.stop()来防止移进移出的山东问题. 二. 1.定义: stop() 方法为被选元素停止当前正在运行的动画. 2.语法: $(selec ...
- CSU-1632 Repeated Substrings (后缀数组)
Description String analysis often arises in applications from biology and chemistry, such as the stu ...
- Python 十进制转二进制代码实现
def my_bin(num): la = [] if num < 0: return '-' + my_bin(abs(num)) while True: num, remainder = d ...
- epoll 反应堆
epoll反应堆模型 ================================ 下面代码实现的思想:epoll反应堆模型:( libevent 网络编程开源库 核心思想) . 普通多路IO转接 ...
- Makefile简易教程
本文部分内容引用: 中文维基百科. 一个简单的Makefile教程. Makefile简介 在软件开发中,make通常被视为一种软件构建工具.该工具主要经由读取一种名为"makefile&q ...