如何杀死退出线程? [英] How to kill Thread on exit?

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

问题描述

父窗体上的一个按钮用于启动线程。
如果父窗体在开发环境中关闭线程不断在后台防止编辑源代码在64位Windows 7平台上运行。
线程必须通过菜单>调试>停止调试被杀死。
什么是编程方式杀死线程当父窗体关闭的正确方法?



 私人无效buttonW_Click(对象发件人,EventArgs五)
{
线程t =新主题(Main.MyThread);
t.Start();
}

私有静态无效MyThread的()
{

}


解决方案

如果你想要的应用程序时,主线程完成退出,你可以让新线程在后台线程:

 线程t =新主题(Main.MyThread); 
t.IsBackground = TRUE;
t.Start();



基本上进程将退出时,所有的前景的线程都已经退出。



请注意,这可能是个坏消息,如果后台线程写入文件时窗体关闭,或类似的东西...


A button on the parent form is used to start the thread. If the parent form is closed in the development environment the thread keeps running in the background preventing edits to the source code on a 64 bit Windows 7 platform. The thread has to be killed by Menu > Debug > Stop Debugging. What is the proper way to programmatically kill the thread when the parent Form is closed?

private void buttonW_Click(object sender, EventArgs e)
{
    Thread t = new Thread(Main.MyThread);
    t.Start();
}

private static void MyThread()
{
    ...
}

解决方案

If you want the app to exit when the main thread has finished, you can just make the new thread a background thread:

Thread t = new Thread(Main.MyThread);
t.IsBackground = true;
t.Start();

Basically the process will exit when all the foreground threads have exited.

Note that this could be bad news if the background thread is writing a file when the form is closed, or something similar...

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

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