C#中线程对控件的访问

namespace ThreadVisitingControl
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void showStuIfo(string no, string name, double score) //本例中的线程要通过这个方法来访问主线程中的控件
{
listBox1.Items.Add("学号:" + no);
listBox1.Items.Add("姓名:" + name);
listBox1.Items.Add("成绩:" + score.ToString());
}
public delegate void stuInfoDelegate(string no, string name, double score); //声明委托类型

private void stuThread() //线程方法
{
Invoke(new stuInfoDelegate(showStuIfo), new object[] { "20101001", "张三", 95.5 }); //线程通过方法的委托执行showStuIfo(),实现对ListBox控件的访问

}
private void button1_Click(object sender, EventArgs e)
{
Thread stuth = new Thread(new ThreadStart(stuThread)); //创建线程
stuth.Start(); //执行线程
}
}
}

原文地址:https://www.cnblogs.com/cnsanshao/p/6979470.html