Can there only be 1 AND ONLY 1 handler for ThreadE

2020-07-18 08:39发布

问题:

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

回答1:

Huh. Apparently so.

According to dotPeek, here's the code for the 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;
  }
}

Notice how, in the remove handler, it uses -= as expected, but in the add handler, 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 the add handler), 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.



标签: .net vb.net