特别是当要求结束一个计算后,再进行绘图时,常会将计算的任务用lock, 最后进行绘图
Thread myThread;
Thread myThread1;
public void Add()
{
{
for (int i = 100; i >= 0; i--)
{
Thread.Sleep(200);
label1.Text = i.ToString();
}
}
}
public void Add1()
{
{
for (int j = 0; j <= 30; j++)
{
Thread.Sleep(200);
label2.Text = j.ToString();
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
myThread = new Thread(new ThreadStart(Add));
myThread1 = new Thread(new ThreadStart(Add1));
myThread.Start();
myThread1.Start();
}
如果没有lock其中的Add 或add1,那么,将动态显示两组数值的变化,
当加上lock后,就会等其中的add或aa1运行结束后,再时行(绘图其它操作)
Thread myThread;
Thread myThread1;
public void Add()
{
//lock (this)
{
for (int i = 100; i >= 0; i--)
{
Thread.Sleep(200);
label1.Text = i.ToString();
}
}
}
public void Add1()
{
lock (this) //Add1结束后,显示Form1,
{
for (int j = 0; j <= 30; j++)
{
Thread.Sleep(200);
label2.Text = j.ToString();
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
myThread = new Thread(new ThreadStart(Add));
myThread1 = new Thread(new ThreadStart(Add1));
myThread.Start();
myThread1.Start();
}