托管改变属性的值InvokeRequired

由于线程安全问题,在多线程编程下更改一个控件的属性时,往往需要用托管来更改属性的值.下面是一个通用的托管,用反射来对属性进行赋值.

public delegate void SetValueCallBack(Control control, string property, object value);
public static void SetValue(Control control, string property, object value)
...{
     if (control.InvokeRequired)
         control.Invoke(new SetValueCallBack(SetValue), new object[] ...{ control, property, value });
     else
     ...{
         PropertyInfo p = control.GetType().GetProperty(property);
         p.SetValue(control, Common.Data.To(value, p.PropertyType), null);
         Application.DoEvents();
     }
}

 this.Invoke((EventHandler)delegate{this.Text = "test";});

posted @ 2013-01-28 18:52  louiskoo  阅读(287)  评论(0编辑  收藏  举报