如何在 C# 中终止线程?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14131608/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to terminate a thread in C#?
提问by Bill Skiadas
I wanted to try my luck in threading with C#, I know a few things about threading in C.
我想试试 C# 线程的运气,我知道一些关于 C 线程的知识。
So I just wanted to ask if i wanted to terminate a thread, I should do it with smt.Abort()
or it will "kill itself" after the function ends?
所以我只是想问我是否想终止一个线程,我应该这样做smt.Abort()
还是在函数结束后它会“自杀”?
Also, is there something like pthread_exit()
in C in C#?
另外,pthread_exit()
在 C# 中是否有类似的东西 ?
采纳答案by JerKimball
Thread.Abort
will "kill" the thread, but this is roughly equivalent to:
Thread.Abort
将“杀死”线程,但这大致相当于:
Scenario: You want to turn off your computer
场景:您想关闭计算机
Solution: You strap dynamite to your computer, light it, and run.
解决方案:将炸药绑在计算机上,点燃它,然后运行。
It's FAR better to trigger an "exit condition", either via CancellationTokenSource.Cancel
, setting some (safely accessed) "is running" bool, etc., and calling Thread.Join
. This is more like:
触发“退出条件”要好得多,通过CancellationTokenSource.Cancel
设置一些(安全访问的)“正在运行”bool 等,然后调用Thread.Join
. 这更像是:
Scenario: You want to turn off your computer
场景:您想关闭计算机
Solution: You click start, shut down, and wait until the computer powers down.
解决方案:单击开始、关闭,然后等待计算机关机。
回答by MikeB
Terminating a thread externally (from outside the thread) is a bad idea; you never know what the thread was in the middle of doing when you kill it asynchronously. In C#, if your thread function returns, the thread ends.
从外部(从线程外部)终止线程是一个坏主意;当你异步杀死它时,你永远不知道线程在做什么。在 C# 中,如果您的线程函数返回,则线程结束。
This MSDN article How to: Create and Terminate Threads (C# Programming Guide)has some notes and some sample code that you will probably find helpful.
这篇 MSDN 文章如何:创建和终止线程(C# 编程指南)有一些注释和一些示例代码,您可能会发现它们很有帮助。
回答by Rudi Visser
You don't need to terminate a thread manually once the function has ended.
一旦函数结束,您就不需要手动终止线程。
If you spawn up a thread to run a method, once the method has returned the thread will be shut down automatically as it has nothing further to execute.*
如果您生成一个线程来运行一个方法,一旦该方法返回该线程将自动关闭,因为它没有进一步的执行。*
You can of course, manually abort a thread by simply calling Abort()
, but this is pretty much un-recommended due to potential thread state corruption due to unreliable determination of where a thread is at in its current execution state. If you need to handle the killing of threads yourself, you may be best looking into using a CancellationToken. You could also read up on the Cancellation of Managed Threadsarticle on MSDN.
当然,您可以通过简单地调用 手动中止线程Abort()
,但由于无法可靠地确定线程在其当前执行状态中的位置而导致潜在的线程状态损坏,因此几乎不推荐这样做。如果您需要自己处理线程的终止,您最好考虑使用CancellationToken。您还可以阅读MSDN上的取消托管线程文章。
** That is, unless, you're using a ThreadPool to perform your work. You shouldn't worry about aborting these threads as they're reused across different queued tasks.
** 也就是说,除非您使用 ThreadPool 来执行您的工作。您不必担心中止这些线程,因为它们会在不同的排队任务中重复使用。
回答by Dean
Thread.Abort()
Thread.Join();
Thread = null;