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;
- }
- public static object InvokeMethodSafe(thisToolStripMenuItem control, string methodName, paramsobject[] args)
- {
- object returnValue = null;
- if (args == null)
- {
- args = newobject[1];
- args[0] = null;
- }
- else if (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();
- }
- returnreturnValue;
- }
- }
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();
}
}
}

浙公网安备 33010602011771号