现有三个lable控件,要控制每个lable随机出现数字(0-9)用线程解决,是定义三个线程,还是一个线程解决?
//方法一
Thread t1;
Thread t2;
Thread t3 ;
bool star=false;
bool strop=false;
bool strop2 = false;
bool strop3 = false;
public void Romand()
{
int time = 50;
Random r = new Random(0);
int a;
while (1 == 1)
{
a = (int)(r.NextDouble() * 9);
if (star)
{
time += 85;
if (time > 600)
{
strop = true;
}
}
Thread.Sleep(time);
if (!strop)
label1.Text = a.ToString();
}
}
public void Romand2()
{
int time2=50;
Random r = new Random(0);
int a;
while (1 == 1)
{
if (star)
{
time2 += 65;
if (time2 >600)
{
strop2 = true;
}
}
a = (int)(r.NextDouble() * 9);
Thread.Sleep(time2);
if (!strop2)
label2.Text = a.ToString();
}
}
public void Romand3()
{
int time3=50;
Random r = new Random(0);
int a;
while (1 == 1)
{
if (star)
{
time3 += 35;
if (time3 >600)
{
strop3 = true;
}
}
a = (int)(r.NextDouble() * 9);
Thread.Sleep(time3);
if(!strop3)
label3.Text = a.ToString();
}
}
//方法二
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;
}
bool star = false;
bool stop = false;
public void getRandom()
{
int time = 50;
Random r = new Random();
while (1 == 1)
{
int a = (int)(r.NextDouble() * 10); // 数值的强转
int b = (int)(r.NextDouble() * 10);
int c = (int)(r.NextDouble() * 10);
if (star)
{
time += 85;
if (time > 600)
{
stop = true;
}
}
Thread.Sleep(time);
if (!stop)
{
this.label1.Text = a.ToString();
this.label2.Text = b.ToString();
this.label3.Text = c.ToString();
}
}
}
请各位高手仔细分析下两者区别?