I need to terminate a frozen thread, I set IsBackground as true but it stays alive. Thread's properties:
ThreadState = AbortRequested
IsBackground = true
When I examine the frozen spot I find the line below:
resultDetect = Detect(input, ref output);
The spot is a 3rd party code (Method Detect). The thread only updates resultDetect as you see. I need to abort that thread and re-start a new one to continue. Otherwise, the application开发者_JAVA技巧 waits and does nothing -fresh resultDetect needed-
How can I kill the thread that doesn't die?
There's only one way to safely kill a hung thread in your application: Environment.Exit And even that can fail if the thread is running kernel code.
It's best not to use third-party code that hangs. If you have no choice, then run it in a separate process.
If Detect
transitions into unmanaged code then the CLR will defer the injection of the ThreadAbortException
until it returns. This behavior changed in .NET 2.0 to make thread aborts a lot safer. The CLR is trying really hard to protect you from corrupting the state of the process which would be very likely in the case of unmanaged code since you do not get the benefit of the isolation of app domains which can be easily unloaded under an all managed scenario. Really, the only solution is to run this API in a separate process and use WCF, remoting, etc. to communicate with it.
Maybe try to call Thread.Abort()
. Although it is not recommended (See Killing a .NET thread)
精彩评论