跨线程访问窗体控件 武胜

public void SetText<T>(T control, string text) where T : Control
{
    if (control.InvokeRequired)
        control.Invoke(new Action<Control, String>(SetText), new Object[] { control, text });
    else
        control.Text = text;
}
 
///////////////
//委托
delegate void SetEnableCallback(System.Windows.Forms.Control objCtrl, bool enable);
//声明
private void _SetText<TObject>(TObject objCtrl, string text)
where TObject : System.Windows.Forms.Control
{
  if (objCtrl.InvokeRequired)
  {
    SetTextCallback d = new SetTextCallback(_SetText);
    this.Invoke(d, new object[] { objCtrl, text });
  }
  else
  {
    objCtrl.Text = text;
  }
}
//调用
public int TotalNum
{
set { _SetText<TextBox>(this.txt_Total, value.ToString()); }
}
原文地址:https://www.cnblogs.com/zeroone/p/3033436.html