如何关闭所有正在运行的线程? [英] How to close all running threads?

查看:197
本文介绍了如何关闭所有正在运行的线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用

  foreach (Form form in Application.OpenForms)
    {

        form.Close();
    }
    Application.Exit();



不退出应用程序。我怎样才能退出应用程序,并确保所有正在运行的线程关闭?

it does not exit the Application. How can I exit the application and make sure all running thread are closed?

推荐答案

您不显示使用任何线程在你的代码,但是让我们假设你确实有它的线程。要关闭所有的线程你应该所有的人设置为后台线程你开始之前,那么他们将被自动关闭应用程序退出时,如:

You don't show the use of any threads in your code, but let's suppose you do have threads in it. To close all your threads you should set all of them to background threads before you start them, then they will be closed automatically when the application exits, e.g.:

Thread myThread = new Thread(...);
myThread.IsBackground = true; // <-- Set your thread to background
myThread.Start(...);

一个HOWTO:停止多线程从微软的文章:http://msdn.microsoft.com/en-us/library/aa457093.aspx

A "HOWTO: Stop Multiple Threads" article from microsoft: http://msdn.microsoft.com/en-us/library/aa457093.aspx

这篇关于如何关闭所有正在运行的线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆