111,126
社区成员
发帖
与我相关
我的任务
分享
private void button1_Click(object sender, EventArgs e)
{
InstanceCaller = new Thread(new ThreadStart(this.InstanceMethod));
InstanceCaller.Start(); // 线程启动
InstanceCaller1 = new Thread(new ThreadStart(this.InstanceMethod1));
InstanceCaller1.Start(); // 线程启动
}
private void InstanceMethod() // 线程的执行函数
{
while (t)
{
lock (thisLock)
{
for (int i=0;i <100;i++)
{
data[i] = i;
}
}
Thread.Sleep(1);
}
}
private void InstanceMethod1() // 线程的执行函数
{
while (t)
{
lock (thisLock)
{
for (int i = 99; i >=0; i--)
{
data[i] = i;
}
}
Thread.Sleep(1);
}
}
private void button2_Click(object sender, EventArgs e)
{
t = false; //停止
InstanceCaller.Abort(); // 关闭
InstanceCaller1.Abort();// 关闭 在执行它时出问题,死锁了
}
Thread InstanceCaller;
Thread InstanceCaller1;
bool t = true;
int []data =new int[100] ;
private Object thisLock = new Object();