如何在Windows应用程序关闭时中止正在运行的线程 [英] How to abort a running thread when the windows application gets closed

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

问题描述

我在.net Windows应用程序中使用线程。当我关闭我的应用程序时,应该停止线程,但它们仍然继续运行。当应用程序关闭时,有没有人知道我们如何杀死线程。



提前谢谢!

解决方案

< blockquote>看一下Stack Overflow上的这个帖子



http://stackoverflow.com/questions/3542061/how-do-i-stop-a-thread-when-my-winform-application-closes [ ^ ]



特别是,第二个答案应该给你一个线索


如果你创建新线程作为后台线程(通过在启动它们之前设置IsBackground),它们将在主线程(应用程序线程)终止时自动停止。



(来自MSDN):



线程是背景线程或前台线程。后台线程与前台线程相同,除了后台线程不会阻止进程终止。一旦属于进程的所有前台线程终止,公共语言运行库就结束该进程。任何剩余的后台线程都会停止并且不会完成。


要么做Dylan已经提出的建议(我推荐),要么你也可以跟踪你在应用程序中启动的线程然后使用Thread.Abort()杀死他们。



最好的问候,


这是Manfred附加说明的补充说明。 :-)



当然你可以使用 Thread.Abort 你应该在某些情况下这样做,但是你需要非常负责任地去做。在所有情况下,您都应该处理 ThreadAbortException 异常。严格来说,如果这是应用程序运行时的最后一部分,您可能会忽略它以及任何相关问题。我不推荐它;它可能会严重损害您的代码的可支持性。



只考虑一件事,例如:如果线程打开一些具有写访问权限的文件并且某些数据需要刷新会发生什么?处理 ThreadAbortException 允许您处理所有post-mortal情况。



有关详细信息,请参阅我的与Olivier讨论这个问题的答案:如何中止已经运行的特定线程? [ ^ ]。请查找子字符串exception seeding以进入该主题。在那里,我试图提供关于它是如何工作的非常少的已知信息。



-SA


I use threads in my .net Windows application. When I close my application then the threads should be stopped, but they still keep on running. Does anybody know the answer how we kill threads when the application gets closed.

Thanks in advance!

解决方案

Have a look at this thread on Stack Overflow

http://stackoverflow.com/questions/3542061/how-do-i-stop-a-thread-when-my-winform-application-closes[^]

In particular, the second answer should give you a clue

" If you create the new threads as background threads (by setting IsBackground before starting them), they will automatically stop when the main thread (the application thread) terminates."

(From MSDN):

A thread is either a background thread or a foreground thread. Background threads are identical to foreground threads, except that background threads do not prevent a process from terminating. Once all foreground threads belonging to a process have terminated, the common language runtime ends the process. Any remaining background threads are stopped and do not complete.


Either do what Dylan already proposed (I recommend that) or you can also keep track of which Threads you started in your application and then use Thread.Abort() to kill them.

Best Regards,


This is the additional note for additional note, by Manfred. :-)

Of course you can use Thread.Abort and you should do it in some situations, but you need to do it very responsibly. In all cases you should handle ThreadAbortException exception. Strictly speaking, if this is the very end of the application run-time, you might ignore it and any related problems. I don't recommend it; it could badly hurt supportability of your code.

Think about just one thing, for example: what happens if you thread opened some files with write access and some data need flush? Handling ThreadAbortException allows you to handle all "post-mortal" situations.

For more detail, please see my discussion with Olivier over his answer to this Question: How do I abort a specific thread that is already running?[^]. Please find a sub-string "exception seeding" to get into the topic. There I tried to provide very little known information on how it works.

—SA


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

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