How does the CLR raise ThreadAbortException?

392 Views Asked by At

If you abort one managed thread from another, using Thread.Abort, how does the CLR actually throw the exception on the other thread? Seems like a neat trick!

2

There are 2 best solutions below

0
On BEST ANSWER

I've found a few interesting links on the subject. ThreadAbortException is a special case, and it is handled specially by the CLR.

http://mnikoo.net/2007/02/07/the-magic-of-the-clr-threadabortexception/

http://blogs.msdn.com/b/clrteam/archive/2009/04/28/threadabortexception.aspx

0
On

I wrote a blog post on this awhile back. The first part is about when a thread can be aborted, the second is about how it actually works.

I hadn't ever seen any correct (in this case, complete) documentation about how it actually works, so I wrote about about it.

The jist is that the CLR will use SetThreadContext (a win32 api) to hijack your current IP and move you into a special stub to set up the thread abort if you're thread isn't in an abortable wait.

Check out the post here