C#线程中安全访问控件(重用委托,避免繁复的delegate,Invoke)总结
2016-09-23 00:38
831 查看
C#线程中安全访问控件(重用委托,避免繁复的delegate,Invoke)总结
1.第一种,不安全,当线程过多后,timer控件和线程中同时访问窗体控件时,有时会出现界面重绘出错。public frmMain() { InitializeComponent(); System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls =false; }
2.避免繁复的delegate,Invoke,转载,不推荐使用
public static class ControlCrossThreadCalls { public delegate void InvokeHandler(); ///<summary> /// 线程安全访问控件,扩展方法 .net 3.5下用Lambda简化跨线程访问窗体控件,避免繁复的delegate,Invoke /// this.SafeInvoke(() => /// { /// tsStatus.Text = one.Email + " 开始任务...."; /// }); ///</summary> //public static void SafeInvoke(this Control control, InvokeHandler handler) //{ // if (control.InvokeRequired) // { // control.Invoke(handler); // } // else // { // handler(); // } //} ///<summary> /// .net2.0线程安全访问扩展方法///</summary> /// ControlCrossThreadCalls.SafeInvoke(this.tsStatus, new ControlCrossThreadCalls.InvokeHandler(delegate() /// { /// tsStatus.Text = one.Email + " 开始任务..."; /// })); public static void SafeInvoke(Control control, InvokeHandler handler) { if (control.InvokeRequired) { control.Invoke(handler); } else { handler(); } } }
3.异步最新,推荐使用
using System; using System.Collections.Generic; using System.Text; using System.Windows.Forms; using System.Threading; /// <summary> /// 线程中安全访问控件,避免重复的delegate,Invoke /// </summary> public static class CrossThreadCalls { public delegate void TaskDelegate(); private delegate void InvokeMethodDelegate(Control control, TaskDelegate handler); /// <summary> /// .net2.0中线程安全访问控件扩展方法,可以获取返回值,可能还有其它问题 /// </summary> /// CrossThreadCalls.SafeInvoke(this.statusStrip1, new CrossThreadCalls.TaskDelegate(delegate() /// { /// tssStatus.Text = "开始任务..."; /// })); /// CrossThreadCalls.SafeInvoke(this.rtxtChat, new CrossThreadCalls.TaskDelegate(delegate() /// { /// rtxtChat.AppendText("测试中"); /// })); /// 参考:http://wenku.baidu.com/view/f0b3ac4733687e21af45a9f9.html /// <summary> public static void SafeInvoke(Control control, TaskDelegate handler) { if (control.InvokeRequired) { while (!control.IsHandleCreated) { if (control.Disposing || control.IsDisposed) return; } IAsyncResult result = control.BeginInvoke(new InvokeMethodDelegate(SafeInvoke), new object[] { control, handler }); control.EndInvoke(result);//获取委托执行结果的返回值 return; } IAsyncResult result2 = control.BeginInvoke(handler); control.EndInvoke(result2); } /// <summary> /// 线程安全访问控件,扩展方法.net3.5用Lambda简化跨线程访问窗体控件,避免重复的delegate,Invoke /// this.statusStrip1.SafeInvoke(() => /// { /// tsStatus.Text = "开始任务...."; /// }); /// this.rtxtChat.SafeInvoke(() => /// { /// rtxtChat.AppendText("测试中"); /// }); /// </summary> //public static void SafeInvoke(this Control control, TaskDelegate handler) //{ // if (control.InvokeRequired) // { // while (!control.IsHandleCreated) // { // if (control.Disposing || control.IsDisposed) // return; // } // IAsyncResult result = control.BeginInvoke(new InvokeMethodDelegate(SafeInvoke), new object[] { control, handler }); // control.EndInvoke(result);//获取委托执行结果的返回值 // return; // } // IAsyncResult result2 = control.BeginInvoke(handler); // control.EndInvoke(result2); //}
更正一个我发现的C#多线程安全访问控件普遍存在的问题,仅供参考,在网上搜索多线程访问控件,发现很多都是这种类似的写法
http://msdn.microsoft.com/zh-cn/library/ms171728.aspx
private void SetText(string text) { // InvokeRequired required compares the thread ID of the // calling thread to the thread ID of the creating thread. // If these threads are different, it returns true. if (this.textBox1.InvokeRequired) { SetTextCallback d = new SetTextCallback(SetText); this.Invoke(d, new object[] { text }); } else { this.textBox1.Text = text; } }
注意红色部分,这样写几个线程同时操作时问题不是很大,但是当我几10个几100个线程频繁操作时,就出现了System.OutOfMemoryException这个异常,猜测可能是线程堵塞,同时造成cpu很高,内存成倍增长。
相关文章推荐
- C#线程中安全访问控件(重用委托,避免繁复的delegate,Invoke)总结
- .NET3.5下用Lambda简化跨线程访问窗体控件,避免繁复的delegate,Invoke(转)
- C#中跨线程访问控件:委托,Invoke,BeginInvo
- C#中跨线程访问控件: 委托, Invoke, BeginInvoke
- C#中跨线程访问控件: 委托, Invoke, BeginInvoke
- C#跨线程修改控件——从MSIL和汇编看Invoke, 多线程, 事件与事件委托
- C# 委托 / 跨线程访问UI / 线程间操作无效: 从不是创建控件“Form1”的线程访问它
- C# 委托 从不是创建控件“richTextBox1”的线程访问它。解决方案,利用委托
- [C#] Control.Invoke方法和跨线程访问控件
- c# InvokeRequired 解决跨线程访问UI控件的问题
- C#中的BackgroundWorker控件+Delegate.Invoke (委托同步调用)
- C#跨线程修改控件——从MSIL和汇编看Invoke, 多线程, 事件与事件委托
- c# winform InvokeRequired 解决跨线程访问控件
- 使用委托及控件的invoke方法处理窗体控件的跨线程访问
- C#用委托(Delegate)的BeginInvoke和EndInvoke方法操作线程
- invoke委托解决“线程间操作无效: 从不是创建控件的线程访问它”的问题
- c# winform下使用多线程时在线程里如何安全访问窗体控件InvokeRequired
- c#中Invoke使用方法(线程间操作无效: 从不是创建控件的线程访问它。那么创建它的线程是哪个线程?)
- c#开启新线程,如果访问主线程的控件,利用委托
- c#之如何安全的跨线程访问控件