c# 多线程委托 环境是 .net framework 3.5(含以上)

publicstaticclass ExtensionControl
{                                               
    publicstaticobject GetPropertySafe(this Control control, string propertyName)
    {
        object returnValue = null;
        Action func = () =>
        {
            Type type = control.GetType();
            returnValue = type.InvokeMember(propertyName, BindingFlags.GetProperty, null, control, null);
        };
        if (control.InvokeRequired)
        {
            control.Invoke(func);
        }
        else
        {
            func();
        }
        return returnValue;
    }
 
    publicstaticobject SetPropertySafe(this Control control, string propertyName, 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();
        }
        return returnValue;
    }
 
    publicstaticobject GetPropertySafe(this ToolStripMenuItem control, string propertyName)
    {
        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();
        }
        return returnValue;
    }
 
    publicstaticobject 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();
        }
        return returnValue;
    }        
 
    publicstaticobject InvokeMethodSafe(this Control control, string methodName, paramsobject[] args)
    {
        object returnValue = null;
        if (args == null)
        {
            args = newobject[1];
            args[0] = null;
        }
        elseif (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();
        }
        return returnValue;
    }
 
    publicstaticobject InvokeMethodSafe(this ToolStripMenuItem control, string methodName, paramsobject[] args)
    {
        object returnValue = null;
        if (args == null)
        {
            args = newobject[1];
            args[0] = null;
        }
        elseif (args != null && args.Length == 0)
        {
            args = null;
        }
        Control owner = control.Owner;
        Action func = () =>
        {
            Type type = control.GetType();
            returnValue = type.InvokeMember(methodName, BindingFlags.InvokeMethod, null, control, args);
        };
        if (owner.InvokeRequired)
        {
            owner.Invoke(func);
        }
        else
        {
            func();
        }
        return returnValue;
    }
}    
 
例子(Form):
namespace WindowsFormsApplication1
{
    publicpartialclass MainForm : Form
    {        
        public MainForm()
        {
            InitializeComponent();
        }
 
        privatevoid button1_Click(object sender, EventArgs e)
        {
            Thread thread = new Thread(
                ThreadStart =>
                {                   
                    this.textBox1.SetPropertySafe("Text", "ThreadEdit");
                }
            );
            thread.IsBackground = true;
            thread.Start();
        }
    }
}


你可能感兴趣的:(多线程,C#,委托)