如何退出所有正在运行的线程? [英] How to exit all running threads?

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

问题描述

以下代码不会退出该应用程序.如何退出应用程序并确保所有正在运行的线程都已关闭?

The following code does not exit the application. How can I exit the application and make sure all the running threads are closed?

foreach (Form form in Application.OpenForms)
{
    form.Close();
}
Application.Exit();

推荐答案

您没有在代码中显示对任何线程的使用,但让我们假设其中确实包含线程.要关闭所有线程,您应在启动它们之前将它们全部设置为后台线程,然后在应用程序退出时它们将自动关闭,例如:

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(...);

Microsoft的"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天全站免登陆