首先在类的开端声明一个委托:
public delegate void MyInvoke(string str);
以一个在textbox显示多行文字 为例 :
private void showLog(string log)
{
if (txtLog.InvokeRequired)
{
MyInvoke _myInvoke = new MyInvoke(showLog);
this.Invoke(_myInvoke, new object[] { log });
}
else
{
txtLog.Text = "[" + System.DateTime.Now.ToLocalTime() + "] " + log + "/r/n" + txtLog.Text;
//txtLog.Text = txtLog.Text + "[" + System.DateTime.Now.ToLocalTime() + "] " + log + "/r/n";
}
}
这样就可以防止死锁了