利用线程和委托访问窗体控件
第一,声明一个委托:
public delegate void myinvole(string str);
第二,创建线程和委托的使用:
private void button3_Click(object sender, EventArgs e)
{
Thread t = new Thread(new ThreadStart(GetTextFun));
t.Start();
}
//线程指向的方法
private void GetTextFun()
{
setText("中华人民共和国!!!!");
}
//创建一个指向委托的方法
private void setText(string str)
{
if (textBox1.InvokeRequired)
{
myinvole _myinvole = new myinvole(setText);
this.Invoke(_myinvole, new object[] { str });
}
else
{
this.textBox1.Text = str;
}
}
{
Thread t = new Thread(new ThreadStart(GetTextFun));
t.Start();
}
//线程指向的方法
private void GetTextFun()
{
setText("中华人民共和国!!!!");
}
//创建一个指向委托的方法
private void setText(string str)
{
if (textBox1.InvokeRequired)
{
myinvole _myinvole = new myinvole(setText);
this.Invoke(_myinvole, new object[] { str });
}
else
{
this.textBox1.Text = str;
}
}