SafeInvoke

 

 

    • public static class  ExtensionControl
    • {                                              
    •       public static object GetPropertySafe(this Control control, string propertyName)
    •     {
    •         objectreturnValue = null;
    •         Action func = () =>
    •         {
    •             Type type = control.GetType();
    •              returnValue = type.InvokeMember(propertyName, BindingFlags.GetProperty, null, control, null);
    •         };
    •         if (control.InvokeRequired)
    •         {
    •              control.Invoke(func);
    •         }
    •         else
    •         {
    •              func();
    •         }
    •           returnreturnValue;
    •     }

 

    • public static object SetPropertySafe(this Control control, stringpropertyName, object value)
    •     {
    •          object  returnValue = null;
    •         Action func = () =>
    •         {
    •             Type type = control.GetType();
    •                   returnValue = type.InvokeMember(propertyName, BindingFlags.SetProperty, null, control, newobject[] { value });
    •         };
    •         if (control.InvokeRequired)
    •         {
    •              control.Invoke(func);
    •         }
    •         else
    •         {
    •               func();
    •         }
    •             returnreturnValue;
    •     }

 

    • public static object GetPropertySafe(this ToolStripMenuItem control, stringpropertyName)
    •     {
    •         object returnValue = null;
    •         Control owner = control.Owner;
    •         Action func = () =>
    •         {
    •             Type type = control.GetType();
    •             returnValue = type.InvokeMember(propertyName, BindingFlags.GetProperty, null, control, null);
    •         };
    •         if (owner.InvokeRequired)
    •         {
    •             owner.Invoke(func);
    •         }
    •         else
    •         {
    •               func();
    •         }
    •               returnreturnValue;
    •     }

 

    • public   static object SetPropertySafe(this.ToolStripMenuItem control, string propertyName, object value)
    •     {
    •          object returnValue = null;
    •         Control owner = control.Owner;
    •         Action func = () =>
    •         {
    •             Type type = control.GetType();
    •             returnValue = type.InvokeMember(propertyName, BindingFlags.SetProperty, null, control, newobject[] { value });               
    •         };
    •        if (owner.InvokeRequired)
    •         {
    •             owner.Invoke(func);
    •         }
    •       else
    •         {
    •              func();
    •         }
    •             returnreturnValue;
    •     }       

 

    • public static object InvokeMethodSafe(this Control control, stringmethodName, paramsobject[] args)
    •     {
    •       object returnValue = null;
    •       if (args == null)
    •         {
    •               args = newobject[1];
    •               args[0] = null;
    •         }
    •       else if (args != null&&args.Length == 0)
    •         {
    •              args = null;
    •         }
    •         Action func = () =>
    •         {
    •             Type type = control.GetType();               
    •              returnValue = type.InvokeMember(methodName, BindingFlags.InvokeMethod, null, control, args);
    •         };
    •        if (control.InvokeRequired)
    •         {
    •              control.Invoke(func);
    •         }
    •        else
    •         {
    •               func();
    •         }
    •              returnreturnValue;
    •     }

 

    1. public static object    InvokeMethodSafe(thisToolStripMenuItem control, string methodName, paramsobject[] args)
    2.     {
    3.       object returnValue = null;
    4.       if (args == null)
    5.         {
    6.             args = newobject[1];
    7.             args[0] = null;
    8.         }
    9.        else if (args != null&&args.Length == 0)
    10.         {
    11.               args = null;
    12.         }
    13.         Control owner = control.Owner;
    14.         Action func = () =>
    15.         {
    16.             Type type = control.GetType();
    17.              returnValue = type.InvokeMember(methodName, BindingFlags.InvokeMethod, null, control, args);
    18.         };
    19.         if (owner.InvokeRequired)
    20.         {
    21.               owner.Invoke(func);
    22.         }
    23.          else
    24.         {
    25.                func();
    26.         }
    27.              returnreturnValue;
    28.     }
    29. }   

       

    30. namespace WindowsFormsApplication1

      {

      publicpartialclassMainForm : Form

          {       

              publicMainForm()

              {

                      InitializeComponent();

              }

       

      privatevoid button1_Click(object sender, EventArgs e)

              {

                  Thread thread = newThread(

                   ThreadStart =>

                      {

                            //this.textBox1.Text = "Thread Edit";

                          //將上方會產生InvalidOperationException的代碼註解,改以下方代碼

                           this.textBox1.SetPropertySafe("Text", "ThreadEdit");

                      }

                  );

                thread.IsBackground = true;

                 thread.Start();

              }

          }

      }

 

posted @ 2015-11-27 08:57  春树  阅读(349)  评论(0)    收藏  举报