1、跨线程访问控件委托和类的定义
class="code_img_closed" src="/Upload/Images/2014101314/0015B68B3C38AA5B.gif" alt="" />logs_code_hide('91b15357-c250-4d72-97cf-862e84dcec7b',event)" src="/Upload/Images/2014101314/2B1B950FA3DF188F.gif" alt="" />using System; using System.Windows.Forms; namespace ahwildlife.Utils { /// <summary> /// 跨线程访问控件的委托 /// </summary> public delegate void InvokeDelegate(); /// <summary> /// 跨线程访问控件类 /// </summary> public class InvokeUtil { /// <summary> /// 跨线程访问控件 /// </summary> /// <param name="ctrl">Form对象</param> /// <param name="de">委托</param> public static void Invoke(Control ctrl, Delegate de) { if (ctrl.IsHandleCreated) { ctrl.BeginInvoke(de); } } } }View Code
2、如何使用
在Form1.cs文件的线程方法中:
InvokeDelegate invokeDelegate = delegate() { button4.Enabled = false; button5.Enabled = false; button6.Enabled = false; button7.Enabled = false; button8.Enabled = false; }; InvokeUtil.Invoke(this, invokeDelegate);View Code