What is the least graceful way to stop a .NET thread?

I test the error handling code and try to ensure that my application and data always fail in a consistent state. I have verified that the code works with the “normal” error input methods, but I want to take another step.

What is the coolest way to kill a .NET thread, giving the least chance for error handling to do its job?

Think, for example, how would you test the CLR Constrained Execution Regions? Or how would I simulate something serious, how to pull out the plug?

+3
source share
6 answers

You can p / call the window API function TerminateThread. But this has a good chance of corrupting an unmanaged state in your program, including the state of the CLR itself. So after that, your process is in an undefined state, and it's probably nice to kill the whole process.

+3
source

The reason for the exception is that it is impossible to catch. Just write a function that calls itself.

+6
source

, Thread.Abort(); ThreadAbortedException .

+2

Thread.Abort. ThreadEbortedException .

, , - , , .

+1

, catch finally, TerminateThread(). , . , . , . pinvoking GetCurrentThread(), .

, Thread.Abort(). , .

+1

The Thread.Abort . , volatile boolean, , invoker (: http://www.interact-sw.co.uk/iangblog/2004/11/12/cancellation)

0
source

All Articles