Little intro:
In complex multithreaded aplication (enterprise service bus ESB), I need to use Thread.Abort, because this ESB accepts user written modules which communicates with hardware security modules. So if this module gets deadlocked or hardware stops responding - i need to just unload this module and rest of this server aplication must keep runnnig.
So there is abort sync mechanism which ensures that code can be aborted only in user section and this section must be marked as AbortAble. If this happen (abort) there is possibility that ThreadAbortException will be thrown in this pieace of code:
public void StopAbortSection()
{
var id = Thread.CurrentThread.ManagedThreadId;
lock (threadIdMap[id])
{
....
}
}
For example module is in AbortSection (entered by calling similar method StartAbortSection) and ServerAplication decides to abort user module, but after this decision but before actual Thread.Abort, module enters NonAbortableSection by calling this method, but lock is actualy taken on that locking object.
So lock will block until Abort is executed but abort can be executed also before reaching this block in this code. But Object with this method is essential and i need to be sure that this pieace of code is safe to abort in any moment (doesnt get corrupted - for example i dont know what happens when reading from Dictionary..).
So i have to mention that threadIdMap is Dictionary(int,ManualResetEvent), and locking object is instance of ManualResetEvent.
I hope you now understad my question. Sorry for its largeness.