private void button4_Click(object sender, EventArgs e)
{
Thread thread1 = new Thread(ShowResult);
thread1.IsBackground = true;
thread1.Start();
}
private void ShowResult()
{
int a = 0;
for (int i = 0; i < 600000000; i++)
{
a = i;
}
if (this.textBox1.InvokeRequired)//是否要对文本框进行跨线程访问。
{
//Invoke:去找创建TextBox的线程(主线程(UI线程)),由主线程完成委托方法的调用。
this.textBox1.Invoke(new Action<TextBox, string>(ShowTextBoxValue), this.textBox1, a.ToString());//Action是无返回值的委托
}
else //否则不是跨线程访问
{
this.textBox1.Text = a.ToString();
}
}
//委托注册的函数,跨线程访问,跨线程修改TextBox的值,其实是通过invoke方法让主线程调用该方法。
private void ShowTextBoxValue(TextBox txt,string value)
{
txt.Text = value;
}