.net 2.0加强了线程的安全性,所以两个线程不能直接访问,你可以通过调用委托,和invoke来进行线程间的访问.
随便写一个:delegate void FormHandler();
private void FormWrite()
{
if (!this.InvokeRequired)
{
MessageBox.Show("你想执行的东西");
}
else
{
FormHandler fhandler = new FormHandler(FormWrite);
this.Invoke(fhandler);
}
}
随便写一个:delegate void FormHandler();
private void FormWrite()
{
if (!this.InvokeRequired)
{
MessageBox.Show("你想执行的东西");
}
else
{
FormHandler fhandler = new FormHandler(FormWrite);
this.Invoke(fhandler);
}
}
如下:
public partial class Form1 : Form
{
delegate void SetTextCallback(string text);//声明更新控件用的代理委托private void SetlbSendStatuText(string text)//定义更新控件的方法
{
if (this.lbSendStatu.InvokeRequired)//lbSendStatu为主窗体中一个label控件
{
SetTextCallback d = new SetTextCallback(SetlbSendStatuText);
this.Invoke(d, new object[] { text });
}
else
{
this.lbSendStatu.Text = text;
}
}
private void myThread()//自定义线程
{
this.SetlbSendStatuText("要更新的肉容");
}
}
创建线程时想传值进去的,可以再建立从线程实体类的时候先传入值