Could there be a 1 AND ONLY handler for ThreadException only?

I don't understand why I only get 1 Message Box in the following case when I run (CTRL-F5 - Run Without Debugger) in VS2010:

Public Class Form1 Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load ' Add the event handler for handling UI thread exceptions to the event. AddHandler Application.ThreadException, AddressOf ThreadExceptionHandler AddHandler Application.ThreadException, AddressOf ThreadExceptionHandler2 Throw New Exception("Ha!") End Sub Private Sub ThreadExceptionHandler(ByVal sender As Object, ByVal e As ThreadExceptionEventArgs) MsgBox("FirstHandler") End Sub Private Sub ThreadExceptionHandler2(ByVal sender As Object, ByVal e As ThreadExceptionEventArgs) MsgBox("SecondHandler") End Sub End Class 
+4
source share
1 answer

Yes. Apparently so.

According to dotPeek , here is the code for add and remove handlers for Application.ThreadException:

 public static event ThreadExceptionEventHandler ThreadException { add { System.Windows.Forms.IntSecurity.AffectThreadBehavior.Demand(); Application.ThreadContext threadContext = Application.ThreadContext.FromCurrent(); lock (threadContext) threadContext.threadExceptionHandler = value; } remove { Application.ThreadContext threadContext = Application.ThreadContext.FromCurrent(); lock (threadContext) threadContext.threadExceptionHandler -= value; } } 

Note that in the remove handler, it uses -= as expected, but in the add handler it just uses = ? You think it should be += , but it looks like it is not.

So, when you use the += operator to add a new event handler (which translates to calling the add handler), WinForms actually replaces the existing handler, rather than adding to it.

Looks like a mistake, simple and simple. If you write this on Connect , post the link here so that others can vote for it.

+4
source

All Articles