如何中止线程

时间:2011-03-06 17:59:47

标签: c# .net multithreading

我使用这样的代码

List<Thread> list = new List<Thread>();

Thread newThread = new Thread(delegate() { stream(string); });
                this.list.Add(newThread);
                newThread.Start();

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            foreach (var item in list)
            {
                item.Abort();
            }
        }

但关闭后,该流程仍在任务管理器中运行,如何退出整个应用程序?

我用Google搜索了很多次,但是所有的例子对我来说都很难理解

4 个答案:

答案 0 :(得分:3)

使用调试器。如有必要,使用工具+附加到流程附加它。 Debug + Break All,Debug + Windows + Threads显示仍在运行的线程。使用Thread.IsBackground属性将代码保留在the left door之后。

答案 1 :(得分:1)

退出应用程序:

 Application.Exit(); 

答案 2 :(得分:1)

foreach (Thread t in threads) t.Join();

答案 3 :(得分:1)

如果将线程作为后台线程运行,则退出主应用程序时线程将停止 像这样:

Thread newThread = new Thread(delegate() { stream(string); });
this.list.Add(newThread);
newThread.isBackground = true;
newThread.Start();
相关问题