I don’t understand why I only get 1 Message Box in the following case when I run using (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
Huh. Apparently so.
According to dotPeek, here’s the code for the
addandremovehandlers for Application.ThreadException:Notice how, in the
removehandler, it uses-=as expected, but in theaddhandler, it just uses=? You’d think that should be+=, but it looks like it is not.So yes, when you use the
+=operator to add a new event handler (which translates to a call to theaddhandler), WinForms is actually replacing the existing handler instead of adding to it.Looks like a bug, plain and simple. If you write this up on Connect, post a link here so others can vote for it.