James L
James L

Reputation: 16874

How does the CLR raise ThreadAbortException?

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!

Upvotes: 1

Views: 387

Answers (2)

Steve
Steve

Reputation: 438

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

Upvotes: 1

James L
James L

Reputation: 16874

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

Upvotes: 1

Related Questions