日期:2014-05-19  浏览次数:20691 次

关于多线程的问题
代码我不全写了,,主要是,我在点击一个BUTTON以后要生成3个线程,而此时,主线程,也就是在运行MAIN函数的这个线程我要他停下来,等这3个线程都运行结束之后,在继续运行主线程

代码;(点击按钮以后所触发的事件) private   void   button1_Click(object   sender,   System.EventArgs   e)
{

this.comboBox1.SelectedIndex   =0;
this.button1.Enabled   =   false;
Thread   t1   =   new   Thread(new   ThreadStart(this.randon1));
t1.Start();
Thread   t2   =   new   Thread(new   ThreadStart(this.randon2));
t2.Start();
Thread   t3   =   new   Thread(new   ThreadStart(this.randon3));
t3.Start();
Thread.Sleep(0);

if(this.pictureBox1.Image.Equals(this.pictureBox2.Image)&&this.pictureBox1.Image.Equals(this.pictureBox3.Image)
&&this.pictureBox2.Image.Equals(this.pictureBox3.Image))
{
int   a   =   int.Parse(this.label3.Text)   +   int.Parse(this.comboBox1.SelectedText)*10;
this.label3.Text   =   a.ToString();
}
else   if(this.pictureBox1.Image.Equals(this.pictureBox2.Image)||this.pictureBox1.Image.Equals(this.pictureBox3.Image)
||this.pictureBox2.Image.Equals(this.pictureBox3.Image))
{
int   a   =     int.Parse(this.label3.Text)   +   int.Parse(this.comboBox1.SelectedText)*3;
this.label3.Text   =   a.ToString();
}
else
{
int   a   =   int.Parse(this.label3.Text)   -   int.Parse(this.comboBox1.SelectedText);
this.label3.Text   =   a.ToString();
}
this.button1.Enabled   =   true;

}


关于Thread.Sleep(0);我已经尝试过了,似乎不起任何作用

------解决方案--------------------
可以将Thread.Sleep(0);替换成:

t1.Join();
t2.Join();
t3.Join();