I have an Unhandled Exception handler that does something like this:
AddHandler Application.ThreadException, AddressOf Application_ThreadException
I was looking at what exactly this means and noticed that the decompiled code for Application shows:
/// <include file='doc\Application.uex' path='docs/doc[@for="Application.ThreadException"]/*' />
/// <devdoc>
/// <para>Occurs when an untrapped thread exception is thrown.</para>
/// </devdoc>
public static event ThreadExceptionEventHandler ThreadException {
add {
Debug.WriteLineIf(IntSecurity.SecurityDemand.TraceVerbose, "AffectThreadBehavior Demanded");
IntSecurity.AffectThreadBehavior.Demand();
ThreadContext current = ThreadContext.FromCurrent();
lock(current) {
current.threadExceptionHandler = value;
}
}
remove {
ThreadContext current = ThreadContext.FromCurrent();
lock(current) {
current.threadExceptionHandler -= value;
}
}
}
So it looks like from the “add” that the last person that subscribes to Application.ThreadException is the one who gets it (it’s not additive as most event handlers are). I don’t want to give up my subscription to this eventhandler no matter what. Can anyone think of a way that I could track when I lose my subscription to the Application.ThreadException so I can steal it back from whoever stole it from me?
I am thinking maybe I can do this with a Timer. Check if someone is already subscribed to this event and it is not me then re-subscribe. I know this is a little hackey… just thinking…