我不明白为什么在VS2010中运行(CTRL-F5 - 无调试器运行)时,我在以下情况下只获得1个消息框:
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
答案 0 :(得分:4)
咦。显然是这样。
根据dotPeek,这里是Application.ThreadException的add
和remove
处理程序的代码:
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;
}
}
请注意remove
处理程序如何按预期使用-=
,但在add
处理程序中,它只使用=
?你认为应该是+=
,但它看起来不是。
所以是的,当你使用+=
运算符添加一个新的事件处理程序(转换为对add
处理程序的调用)时,WinForms实际上是替换现有的处理程序,而不是添加到它。
看起来像一个简单明了的bug。如果你在Connect上写下来,请在此处发布链接,以便其他人可以投票。