如果在C#中花费太长时间,我如何杀死一个线程?

时间:2021-12-13 06:40:52

Duplicate: Killing a thread (C#)

重复:杀死一个帖子(C#)


I'm currently passing my thread a threadstart delegate and starting it.

I need a way to kill this thread if I don't receive a response back from it within 3 seconds.

如果我在3秒内没有收到回复,我需要一种方法来杀死这个线程。

What is the best way to accomplish this? It doesn't have to be with thread/threadstart necessarily, that's just what I've been using so far.

完成此任务的最佳方法是什么?它不一定必须与thread / threadstart一起使用,这就是我到目前为止所使用的内容。

4 个解决方案

#1


Check out this SO-solution using the IAsyncResult.AsyncWaitHandle.WaitOne() method

使用IAsyncResult.AsyncWaitHandle.WaitOne()方法查看此SO解决方案

#2


Killing a thread is always a bad idea, and should be reserved for scenarios where you are already dealing with a sickly process you are about to kill anyway. Do you own the code in the thread? Perhaps put in some state checks and exit cleanly if the flag is set?

杀死一个线程总是一个坏主意,并且应该保留用于你已经在处理你将要杀死的病态进程的场景。你是否拥有线程中的代码?也许放入一些状态检查并在标志设置时干净地退出?

If not, and you need the code to work after the abort, a separate process may be safer than a thread-start. At least you won't cripple your own code (with, for example, an unreleased lock, or a hung static constructor / type initializer).

如果没有,并且您需要在中止后使用代码,则单独的进程可能比线程启动更安全。至少你不会削弱你自己的代码(例如,一个未释放的锁,或一个挂起的静态构造函数/类型初始化程序)。

#3


You can call myThread.Abort, which will (usually) kill the thread, but this is usually a very bad idea.

你可以调用myThread.Abort,它会(通常)杀死线程,但这通常是一个非常糟糕的主意。

It's usually a better option to put the abortion logic into the thread itself. If the thread cannot process its work in the 3 second time period, it could abort itself and return the appropriate state. This is a much safer operation, and will be more maintainable in the long run.

将堕胎逻辑放入线程本身通常是更好的选择。如果线程无法在3秒的时间段内处理其工作,则它可以中止并返回适当的状态。这是一个更安全的操作,从长远来看将更易于维护。

#4


The Thread.Join method is supposed to have a TimeSpan parameter to cause it to exit...
Look for TimeSpan and other synchronization methods on this page.

Thread.Join方法应该有一个TimeSpan参数,使其退出...在此页面上查找TimeSpan和其他同步方法。

#1


Check out this SO-solution using the IAsyncResult.AsyncWaitHandle.WaitOne() method

使用IAsyncResult.AsyncWaitHandle.WaitOne()方法查看此SO解决方案

#2


Killing a thread is always a bad idea, and should be reserved for scenarios where you are already dealing with a sickly process you are about to kill anyway. Do you own the code in the thread? Perhaps put in some state checks and exit cleanly if the flag is set?

杀死一个线程总是一个坏主意,并且应该保留用于你已经在处理你将要杀死的病态进程的场景。你是否拥有线程中的代码?也许放入一些状态检查并在标志设置时干净地退出?

If not, and you need the code to work after the abort, a separate process may be safer than a thread-start. At least you won't cripple your own code (with, for example, an unreleased lock, or a hung static constructor / type initializer).

如果没有,并且您需要在中止后使用代码,则单独的进程可能比线程启动更安全。至少你不会削弱你自己的代码(例如,一个未释放的锁,或一个挂起的静态构造函数/类型初始化程序)。

#3


You can call myThread.Abort, which will (usually) kill the thread, but this is usually a very bad idea.

你可以调用myThread.Abort,它会(通常)杀死线程,但这通常是一个非常糟糕的主意。

It's usually a better option to put the abortion logic into the thread itself. If the thread cannot process its work in the 3 second time period, it could abort itself and return the appropriate state. This is a much safer operation, and will be more maintainable in the long run.

将堕胎逻辑放入线程本身通常是更好的选择。如果线程无法在3秒的时间段内处理其工作,则它可以中止并返回适当的状态。这是一个更安全的操作,从长远来看将更易于维护。

#4


The Thread.Join method is supposed to have a TimeSpan parameter to cause it to exit...
Look for TimeSpan and other synchronization methods on this page.

Thread.Join方法应该有一个TimeSpan参数,使其退出...在此页面上查找TimeSpan和其他同步方法。