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 ...
随机推荐
- tinyxml一个优秀的C++ XML解析器
读取和设置xml配置文件是最常用的操作,试用了几个C++的XML解析器,个人感觉TinyXML是使用起来最舒服的,因为它的API接口和Java的十分类似,面向对象性很好. TinyXML是一个开源的解 ...
- C++学习之路——1.linux下环境搭建
学习之路,可说各有各的看法和方法.对于我来说,完全是兴趣一下子来了,脑壳发热吧.就想有个干净的环境,只想着与程序有关的东西练一练. 目前想学习C++,可是打开VC++6,再安了VS2010.VS201 ...
- 转发一篇C++编译器环境搭建的文章
一.环境配置 1. 下载安装MinGW 地址 http://sourceforge.net/projects/mingw/ 2. 设置环境变量 在path变量中, ...
- python数据结构
. 数据结构¶ .1. 深入列表¶ 链表类型有很多方法,这里是链表类型的所有方法: list.append(x) 把一个元素添加到链表的结尾,相当于 a[len(a):] = [x] . list ...
- 使用sublime编写c/c++ 总结
大块头IDE Visual studio太大了,记事本也能写代码但无疑是装逼过分了.写一些轻量级的c/c++代码使用sublime来写是个很好的选择. 三步走: 编译器(win下安装了vs就使用cl, ...
- 二叉树[C实现]
#include<stdio.h> #include<malloc.h> #include<iostream> //定义节点 typedef struct BiNo ...
- python向数据库插入数据时出现乱码解决方案
中文字符串前面加u 如: title =u"你好" contents = "m" ids="13" cur.execute("IN ...
- 深入剖析ConcurrentHashMap(2)
转载自并发编程网 – ifeve.com本文链接地址: 深入剖析ConcurrentHashMap(2) 经过之前的铺垫,现在可以进入正题了.我们关注的操作有:get,put,remove 这3个操作 ...
- Scrollview嵌套listview
//建立Scrollview类 public class MyScrollView extends ScrollView { public MyScrollView(Context context, ...
- 用JS实现的类似QQ密码的输入特效
<input ID="_PSD"></input> <script> function passwordText(ID,pd,mark){ ma ...