當我單擊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