读书人

c# 多线程有关问题怎么终止特定线程

发布时间: 2012-04-28 11:49:53 作者: rapoo

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] 该干嘛就干嘛

读书人网 >C#

热点推荐