1.线程
private void button1_Click(object sender, EventArgs e)
{
ThreadStart threadStart=new ThreadStart(Calculate);//通过ThreadStart委托告诉子线程讲执行什么方法
Thread thread=new Thread(threadStart);
thread.Start();
}
public void Calculate()
{
double Diameter = 0.5;
double result = Diameter * Math.PI;
//CalcFinished(result);//计算完成需要在一个文本框里显示
this.BeginInvoke(new changeText(CalcFinished), result);//计算完成需要在一个文本框里显示
}
delegate void changeText(double result);
public void CalcFinished(double result)
{
if (this.InvokeRequired)
{
this.BeginInvoke(new changeText(CalcFinished), result);
}
else
{
this.textBox1.Text = result.ToString();
}
}
//public void CalcFinished(double result)
//{
// textBox1.Text = result.ToString();//会抛出错误
//}