c# 多线程问题,如何终止特定线程
C# code
private void button1_Click(object sender, EventArgs e)
{
for ( int i=0;i<3;i++)
{
Thread th = new Thread(delegate() { Test(i.ToString()); });
Thread.Sleep(1000);
th.Start();
}
}
void Test(string hwnd)
{
do
{
MessageBox.Show(hwnd);
} while (true);
}
如上代码启动多线程, 问题是我如何终止某一个特定的线程?
------解决方案--------------------
Thread[] threads = new Thread[3];//全局变量
private void button1_Click(object sender, EventArgs e)
{
for ( int i=0;i<threads.Length;i++)
{
threads[i] = new Thread(delegate() { Test(i.ToString()); });
threads[i].Start();
}
}
void Test(string hwnd)
{
do
{
MessageBox.Show(hwnd);
} while (true);
}
//threads[1],threads[2] 该干嘛就干嘛