InvokeHelper:多线程修改主界面控件属性并调用其中方法
生活随笔
收集整理的這篇文章主要介紹了
InvokeHelper:多线程修改主界面控件属性并调用其中方法
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
挺不錯的方法,先網摘過來留個記號http://blog.csdn.net/conmajia/article/details/7831251
?
/******************************************************************************** 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 delegatesprivate 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// helpersprivate 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();elset = 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;}elsethrow new ArgumentNullException("Invalid argument.");}// outlinespublic 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);}elsemi = control.GetType().GetMethod(methodName);// check method info you getif (mi != null)return mi.Invoke(control, args);elsethrow new InvalidOperationException("Invalid method.");}elsethrow 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);elsethrow new FieldAccessException(string.Format("{0}.{1} is a write-only property.",invokee.GetType().ToString(),propertyName));return null;}elsethrow 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);elsethrow new FieldAccessException(string.Format("{0}.{1} is a read-only property.",invokee.GetType().ToString(),propertyName));}elsethrow new ArgumentNullException("Invalid argument.");}#endregion} }?
轉載于:https://www.cnblogs.com/daming1233/p/6432908.html
總結
以上是生活随笔為你收集整理的InvokeHelper:多线程修改主界面控件属性并调用其中方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Codis 3.x 部署安装
- 下一篇: Facebook、LinkedIn、Ai