如何在 C# 中立即杀死一个线程?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1327102/
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 kill a thread instantly in C#?
提问by Mobin
I am using the thread.Abort
method to kill the thread, but it not working. Is there any other way of terminating the thread?
我正在使用该thread.Abort
方法杀死线程,但它不起作用。还有其他终止线程的方法吗?
private void button1_Click(object sender, EventArgs e)
{
if (Receiver.IsAlive == true)
{
MessageBox.Show("Alive");
Receiver.Abort();
}
else
{
MessageBox.Show("Dead");
Receiver.Start();
}
}
I am using this but every time I get the Alive
status, Receiver
is my global thread.
我正在使用它,但每次获得Alive
状态时,Receiver
都是我的全局线程。
采纳答案by redtuna
The reason it's hard to just kill a thread is because the language designers want to avoid the following problem: your thread takes a lock, and then you kill it before it can release it. Now anyone who needs that lock will get stuck.
仅仅杀死一个线程之所以困难,是因为语言设计者想要避免以下问题:您的线程获得一个锁,然后在它释放它之前将其杀死。现在任何需要那个锁的人都会被卡住。
What you have to do is use some global variable to tell the thread to stop. You have to manually, in your thread code, check that global variable and return if you see it indicates you should stop.
您需要做的是使用一些全局变量来告诉线程停止。您必须在您的线程代码中手动检查该全局变量,如果您看到它表明您应该停止,则返回。
回答by Wael Dalloul
thread will be killed when it finish it's work, so if you are using loops or something else you should pass variable to the thread to stop the loop after that the thread will be finished.
线程在完成工作时将被终止,因此如果您正在使用循环或其他东西,您应该将变量传递给线程以在线程完成后停止循环。
回答by Sesh
C# Thread.Abort is NOT guaranteed to abort the thread instantaneously. It will probably work when a thread calls Abort on itself but not when a thread calls on another.
C# Thread.Abort 不能保证立即中止线程。当一个线程对自身调用 Abort 时它可能会工作,但当一个线程调用另一个线程时则不会。
Please refer to the documentation: http://msdn.microsoft.com/en-us/library/ty8d3wta.aspx
请参考文档:http: //msdn.microsoft.com/en-us/library/ty8d3wta.aspx
I have faced this problem writing tools that interact with hardware - you want immediate stop but it is not guaranteed. I typically use some flags or other such logic to prevent execution of parts of code running on a thread (and which I do not want to be executed on abort - tricky).
我在编写与硬件交互的工具时遇到了这个问题 - 您希望立即停止,但不能保证。我通常使用一些标志或其他此类逻辑来防止执行在线程上运行的部分代码(并且我不想在中止时执行 - 棘手)。
回答by Adrian Regan
You should first have some agreed method of ending the thread. For example a running_ valiable that the thread can check and comply with.
您应该首先有一些商定的结束线程的方法。例如,线程可以检查并遵守的 running_ 变量。
Your main thread code should be wrapped in an exception block that catches both ThreadInterruptException and ThreadAbortException that will cleanly tidy up the thread on exit.
您的主线程代码应该包含在一个异常块中,该异常块同时捕获 ThreadInterruptException 和 ThreadAbortException,这将在退出时干净地整理线程。
In the case of ThreadInterruptException you can check the running_ variable to see if you should continue. In the case of the ThreadAbortException you should tidy up immediately and exit the thread procedure.
在 ThreadInterruptException 的情况下,您可以检查 running_ 变量以查看是否应该继续。在出现 ThreadAbortException 的情况下,您应该立即整理并退出线程过程。
The code that tries to stop the thread should do the following:
尝试停止线程的代码应执行以下操作:
running_ = false;
threadInstance_.Interrupt();
if(!threadInstance_.Join(2000)) { // or an agreed resonable time
threadInstance_.Abort();
}
回答by Nickon
You can kill instantly doing it in that way:
你可以用这种方式立即杀死:
private Thread _myThread = new Thread(SomeThreadMethod);
private void SomeThreadMethod()
{
// do whatever you want
}
[SecurityPermissionAttribute(SecurityAction.Demand, ControlThread = true)]
private void KillTheThread()
{
_myThread.Abort();
}
I always use it and works for me:)
我总是使用它并为我工作:)