当我单击button应打印A S,但是当我点击按钮2需要停止线程1,需要启动线程2,什么是错在这里请大家帮我如何创建一个线程
private void button1_Click(object sender, EventArgs e)
{
if (thread2.IsAlive)
{
thread2.Suspend();
}
thread1 = new Thread(threadOne);
thread1.Start();
}
private void button2_Click(object sender, EventArgs e)
{
if (thread1.IsAlive)
{
thread1.Suspend();
}
thread2 = new Thread(threadTwo);
thread2.Start();
}
private void threadOne() {
for (int i=0; i < 20; i++)
{
Console.Write("A");
Thread.Sleep(500);
}
}
private void threadTwo()
{
for (int i = 0; i < 20; i++)
{
Console.Write("B");
Thread.Sleep(500);
}
}
非常感谢 – mssb 2012-07-28 17:55:51
@mssb您应该通过点击答案左上角的绿色勾号来接受答案(如果答案对您有帮助),因此它可以帮助未来的访问者快速找到答案。 – nawfal 2012-07-29 08:13:17