InvokeHelper,让跨线程访问/修改主界面控件不再麻烦(转)
http://bbs.csdn.net/topics/390162519
事实上,本文内容很简单且浅显,所以取消前戏,直接开始。。
源代码:在本文最后
这里是一张动画,演示在多线程(无限循环+Thread.Sleep)情况下主界面操作不受影响。

多线程是一种提高程序运行效率和性能的常用技术。随着我们学习工作的深入,在编程中或多或少会涉及到需要多线程的情况。多数时候,我们的操作模式是后台线程中处理数据,计算结果,然后在前台界面(GUI)中更新显示。
在.NET Framework中,为了保证线程安全,避免出现访问竞争等问题,是不允许跨线程访问窗体控件的。如果强行访问,则会引发InvalidOperationException无效操作异常,如下图:
为了实现跨线程访问控件,.NET Framework为每个控件提供了InvokeRequired属性和Invoke方法。使用这些技巧,就可以实现我们在其他线程中直接修改界面的需要。看起来似乎很简单,但实际每次调用都有不少代码需要编写,还需要自行处理各种异常。下面是典型的调用例子:
|
1
2
3
4
5
6
7
8
9
10
11
|
public void DoWork() { if (control.InvokeRequired) { control.Invoke(DoWork); } else { // do work } } |
为了便于使用,我封装了实现细节,在这里给出一个InvokeHelper类,使用该类即可方便地实现跨线程调用主界面控件方法、获取/设置控件属性等功能。
该类实现非常简单,有效代码约150行,主要有以下3个方法:
1.Invoke
该方法可以调用主界面控件的某个方法,并返回方法执行结果。用法如下:
|
1
|
InvokeHelper.Invoke(<控件>, "<方法名称>", <参数>); |
其中“参数”为参数列表,支持0个或多个参数。
2.Get
该方法可以获取主界面控件的某个属性。用法如下:
|
1
|
InvokeHelper.Get(<控件>, "<属性名称>"); |
3.Set
该方法可以设置主界面控件的某个属性。用法如下:
|
1
|
InvokeHelper.Set(<控件>, "<属性名称>", <属性值>); |
下面是整个类的实现代码。最后是一个演示用的例子。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
|
/******************************************************************************* * InvokeHelper.cs * A thread-safe control invoker helper class. * ----------------------------------------------------------------------------- * Project:Conmajia.Controls * Author:Conmajia * Url:conmajia@gmail.com * History: * 4th Aug., 2012 * Added support for "Non-control" controls (such as ToolStripItem). * * 4th Aug., 2012 * Initiated. ******************************************************************************/ using System; using System.Collections.Generic; using System.Reflection; using System.Text; using System.Windows.Forms; namespace InvokerHelperDemo { /// <summary> /// A thread-safe control invoker helper class. /// </summary> public class InvokeHelper { #region delegates private delegate object MethodInvoker(Control control, string methodName, params object[] args); private delegate object PropertyGetInvoker(Control control, object noncontrol, string propertyName); private delegate void PropertySetInvoker(Control control, object noncontrol, string propertyName, object value); #endregion #region static methods // helpers private static PropertyInfo GetPropertyInfo(Control control, object noncontrol, string propertyName) { if (control != null && !string.IsNullOrEmpty(propertyName)) { PropertyInfo pi = null; Type t = null; if (noncontrol != null) t = noncontrol.GetType(); else t = control.GetType(); pi = t.GetProperty(propertyName); if (pi == null) throw new InvalidOperationException( string.Format( "Can't find property {0} in {1}.", propertyName, t.ToString() )); return pi; } else throw new ArgumentNullException("Invalid argument."); } // outlines public static object Invoke(Control control, string methodName, params object[] args) { if (control != null && !string.IsNullOrEmpty(methodName)) if (control.InvokeRequired) return control.Invoke( new MethodInvoker(Invoke), control, methodName, args ); else { MethodInfo mi = null; if (args != null && args.Length > 0) { Type[] types = new Type[args.Length]; for (int i = 0; i < args.Length; i++) { if (args[i] != null) types[i] = args[i].GetType(); } mi = control.GetType().GetMethod(methodName, types); } else mi = control.GetType().GetMethod(methodName); // check method info you get if (mi != null) return mi.Invoke(control, args); else throw new InvalidOperationException("Invalid method."); } else throw new ArgumentNullException("Invalid argument."); } public static object Get(Control control, string propertyName) { return Get(control, null, propertyName); } public static object Get(Control control, object noncontrol, string propertyName) { if (control != null && !string.IsNullOrEmpty(propertyName)) if (control.InvokeRequired) return control.Invoke(new PropertyGetInvoker(Get), control, noncontrol, propertyName ); else { PropertyInfo pi = GetPropertyInfo(control, noncontrol, propertyName); object invokee = (noncontrol == null) ? control : noncontrol; if (pi != null) if (pi.CanRead) return pi.GetValue(invokee, null); else throw new FieldAccessException( string.Format( "{0}.{1} is a write-only property.", invokee.GetType().ToString(), propertyName )); return null; } else throw new ArgumentNullException("Invalid argument."); } public static void Set(Control control, string propertyName, object value) { Set(control, null, propertyName, value); } public static void Set(Control control, object noncontrol, string propertyName, object value) { if (control != null && !string.IsNullOrEmpty(propertyName)) if (control.InvokeRequired) control.Invoke(new PropertySetInvoker(Set), control, noncontrol, propertyName, value ); else { PropertyInfo pi = GetPropertyInfo(control, noncontrol, propertyName); object invokee = (noncontrol == null) ? control : noncontrol; if (pi != null) if (pi.CanWrite) pi.SetValue(invokee, value, null); else throw new FieldAccessException( string.Format( "{0}.{1} is a read-only property.", invokee.GetType().ToString(), propertyName )); } else throw new ArgumentNullException("Invalid argument."); } #endregion } } |
下面是一个演示用的例子。在该例子中,创建了一个永久循环的线程,该线程每隔500毫秒修改一次界面显示。主要代码如下:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
Thread t; private void button1_Click(object sender, EventArgs e) { if (t == null) { t = new Thread(multithread); t.Start(); label4.Text = string.Format( "Thread state:\n{0}", t.ThreadState.ToString() ); } } public void DoWork(string msg) { this.label3.Text = string.Format("Invoke method: {0}", msg); } int count = 0; void multithread() { while (true) { InvokeHelper.Set(this.label1, "Text", string.Format("Set value: {0}", count)); InvokeHelper.Set(this.label1, "Tag", count); string value = InvokeHelper.Get(this.label1, "Tag").ToString(); InvokeHelper.Set(this.label2, "Text", string.Format("Get value: {0}", value)); InvokeHelper.Invoke(this, "DoWork", value); Thread.Sleep(500); count++; } } |
详细代码请参阅源代码。运行后效果正常,尽管线程t是无限循环的线程,但主界面并不受其阻塞,操作一切正常。
源代码:点击下载
InvokeHelper,让跨线程访问/修改主界面控件不再麻烦(转)的更多相关文章
- winform 跨线程访问问题
一.问题描述 进行winform 开发我们在进行数据交换时避免不了使用多线程或异步方法,这样操作也将避免不了跨线程对控件进行操作(赋值.修改属性). 下面通过一个测试说明一下问题 点击一个按钮异步对t ...
- wpf(怎么跨线程访问wpf控件)
在编写代码时,我们经常会碰到一些子线程中处理完的信息,需要通知另一个线程(我这边处理完了,该你了). 但是当我们通知WPF的UI线程时需要用到Dispatcher. 首先我们需要想好在UI控件上需要显 ...
- C#多线程操作界面控件的解决方案(转)
C#中利用委托实现多线程跨线程操作 - 张小鱼 2010-10-22 08:38 在使用VS2005的时候,如果你从非创建这个控件的线程中访问这个控件或者操作这个控件的话就会抛出这个异常.这是微软为了 ...
- MFC中 自定义类访问主对话框控件的方法
之前一直在找有木有好点的方法.现在终于被我找到,收藏之~~~~~~ 在使用mfc的时候经常遇到自定义类访问主对话框控件的问题,例如自定义类中的方法要输出一段字符串到主对话框的EDIT控件.控制对话框的 ...
- c#使用MethodInvoker解决跨线程访问控件
功能函数测试集锦(77) C#专区(114) 版权声明:本文为博主原创文章,未经博主允许不得转载. .net 原则上禁止跨线程访问控件,因为这样可能造成错误的发生,有一种方法是禁止编译器对跨线 ...
- C#之Winform跨线程访问控件
C#中禁止跨线程直接访问控件,InvokeRequired是为了解决这个问题而产生的,当一个控件的InvokeRequired属性值为真时,说明有一个创建它以外的线程想访问它.此时它将会在内部调用ne ...
- C# 关于跨线程访问控件问题
跨线程访问控件问题的原因是:控件都是在主线程中创建的,而系统默认控件的修改权归其创建线程所有.在子线程中如果需要直接修改控件的内容,需要使用委托机制将控件的修改操作交给主线程处理.因此,当没有使用委托 ...
- .NET设计篇08-线程统一取消模型和跨线程访问UI
知识需要不断积累.总结和沉淀,思考和写作是成长的催化剂,输出倒逼输入 内容目录 一.线程统一取消模型1.取消令牌2.可以中断的线程1.设计一个中断函数2.创建CancellationTokenSour ...
- .NET设计篇08-线程取消模型和跨线程访问UI
知识需要不断积累.总结和沉淀,思考和写作是成长的催化剂,输出倒逼输入 内容目录 一.线程统一取消模型1.取消令牌2.可以中断的线程1.设计一个中断函数2.创建CancellationTokenSour ...
随机推荐
- UVALive 3486/zoj 2615 Cells(栈模拟dfs)
这道题在LA是挂掉了,不过还好,zoj上也有这道题. 题意:好大一颗树,询问父子关系..考虑最坏的情况,30w层,2000w个点,询问100w次,貌似连dfs一遍都会TLE. 安心啦,这肯定是一道正常 ...
- [反汇编练习] 160个CrackMe之003
[反汇编练习] 160个CrackMe之003. 本系列文章的目的是从一个没有任何经验的新手的角度(其实就是我自己),一步步尝试将160个CrackMe全部破解,如果可以,通过任何方式写出一个类似于注 ...
- Java [Leetcode 257]Binary Tree Paths
题目描述: Given a binary tree, return all root-to-leaf paths. For example, given the following binary tr ...
- shell -Z- d等等代表
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 [ -a FILE ] ...
- ios第三方开源库
1.AFNetworking 目前比较推荐的iOS网络请求组件,默认网络请求是异步,通过block回调的方式对返回数据进行处理. 2.FMDB 对sqlite数据库操作进行了封装,demo也比较简单. ...
- XAMPP for Linux
XAMPP 的 Linux 版图片集锦 安装过程仅 4 个步骤 步骤 1:下载 XAMPP PHP 5.4 XAMPP PHP 5.5 步骤 2:安装 步骤 3:开始运行 步骤 4:测试 使 ...
- MBR与GRUB简介
在坛子里找到一篇关于grub和mbr工作原理的文章,以前一直都是一头雾水,今天转这文章学习下..哈.. 能正常工作的grub应该包 括一下文件:stage1.stage2.*stage1_5.menu ...
- C# chart,有关如何在鼠标移动到Series上时显示节点及数据 (有待继续更新)
一.效果与思路 效果: 解决方案1 用chart的mousemove时间,实时跟踪鼠标最近的X轴的位置,然后把cursorX设置到那个位置上,让用户知道我是选的那一个X的值,同时用tooltip显示该 ...
- DevExpress控件XtraGrid的Master-Detail用法 z
XtraGrid支持Master-Detail展示,在自带的Demo中展示了一个“公司——产品——订单”的例子.自己照着实现了一下,有几处关键地方补充一下. 示例: 部门信息(主1)——部门下用户(从 ...
- CABasicAnimation
几个可以用来实现热门APP应用PATH中menu效果的几个方法 +(CABasicAnimation *)opacityForever_Animation:(float)time //永久闪烁的动画 ...