典型的跨线程访问界面控件的问题,需要用到委托
....
delegate void SetTextCallback(string text);...
//this.listBoxState.Items.Add("与服务器断开连接!");
this.SetText("与服务器断开连接!");
break;
...
private void SetText(string text)
{
// InvokeRequired required compares the thread ID of the
// calling thread to the thread ID of the creating thread.
// If these threads are different, it returns true.
if (this.listBoxState.InvokeRequired)
{
SetTextCallback d = new SetTextCallback(SetText);
this.Invoke(d, new object[] { text });
}
else
{
this.listBoxState.Items.Add(text);
}
}
....
delegate void SetTextCallback(string text);...
//this.listBoxState.Items.Add("与服务器断开连接!");
this.SetText("与服务器断开连接!");
break;
...
private void SetText(string text)
{
// InvokeRequired required compares the thread ID of the
// calling thread to the thread ID of the creating thread.
// If these threads are different, it returns true.
if (this.listBoxState.InvokeRequired)
{
SetTextCallback d = new SetTextCallback(SetText);
this.Invoke(d, new object[] { text });
}
else
{
this.listBoxState.Items.Add(text);
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货