分享一多程委派的代
一般我在作多程存取主程控制性,必以委派方式存取,否就有可能出 InvalidOperationException 例外,一般初者犯,大都不知道需要委派的作,是在子程用一行 textBox1.Text = "aaaaa" 就想改控制的性, InvalidOperationException 的,而在一般情下委派的作不像 textBox1.Text = "aaaaa" 短短一行就直的解,得委派代在麻。如果你作的境是 .net framework 3.5(含以上),又得委派的代是惹人的,下面的代或得上你解人的委派,至少了我很多,分享大家,以下是例。
首先定一,然是 Control Class 充方法。
- C# code
public static class ExtensionControl{ public static object 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; } public static object 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, new object[] { value }); }; if (control.InvokeRequired) { control.Invoke(func); } else { func(); } return returnValue; } public static object 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; } 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, new object[] { value }); }; if (owner.InvokeRequired) { owner.Invoke(func); } else { func(); } return returnValue; } public static object InvokeMethodSafe(this Control control, string methodName, params object[] args) { object returnValue = null; if (args == null) { args = new object[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(); } return returnValue; } public static object InvokeMethodSafe(this ToolStripMenuItem control, string methodName, params object[] args) { object returnValue = null; if (args == null) { args = new object[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(); } return returnValue; }}
中分定 SetPropertySafe, GetPropertySafe, InvokeMethodSafe 方法,作是 Control 的充方法,在一 WindowsFormsApplication 加入代或者自己做成 dll 引用都可以,以下是多程作委派的示例。
- C# code
namespace WindowsFormsApplication1{ public partial class MainForm : Form { public MainForm() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { Thread thread = new Thread( ThreadStart => { //this.textBox1.Text = "Thread Edit"; //上方生 InvalidOperationException 的代解,改以下方代 this.textBox1.SetPropertySafe("Text", "ThreadEdit"); } ); thread.IsBackground = true; thread.Start(); } }}
就底解多程存取主程控制委派的麻代,同一行程式做相同的事,接著就可以一反三再充 Control 方法,技上有多深,只是一比方便的代,希望能一些人在作多程委派,能少走路,一同程式加油。
[解决办法]
这个得学习一下
[解决办法]
不错,谢谢分享,自己写扩展方法是个好办法,学习了
[解决办法]
慢慢来研究,不能让他沉了!
[解决办法]
学习中。
[解决办法]
支持原创,谢谢分享!