事件处理程序未自行删除?
在 VB .NET 函数的开头,我删除了一个事件处理程序,并在函数末尾再次添加它,因为函数的代码将触发这些事件,而我不希望它们在函数运行期间触发。这通常有效,但我遇到过一些情况,即使我已经删除了事件,事件仍然被调用。有时在函数开始时删除它两次可以修复它,但其他时候无论我删除它多少次,它仍然会触发。关于可能导致这种情况的原因有什么想法吗?
编辑
代码位于具有虚拟模式datagridview的表单中。我想运行一些操作来触发 datagridview 的 CellValueNeeded 事件,而不触发该事件(因为它会干扰)。
Public Sub DoEventRaisingStuff()
RemoveHandler grid.CellValueNeeded, AddressOf grid_CellValueNeeded
'Do things that would trigger CellValueNeeded
AddHandler grid.CellValueNeeded, AddressOf grid_CellValueNeeded
End Sub
多次删除处理程序不会阻止事件触发,因此它似乎不会被意外地多次添加到其他地方。
有没有办法找出哪些事件处理程序处于活动状态?
At the beginning of a VB .NET function I remove an event handler and add it again at the end of the function because the function's code would trigger those events and I do not want them to trigger for the duration of the function. This usually works, but I have run into a few situations where the event still gets called even though I have already removed it. Sometimes removing it twice at the beginning of the function fixes it, but other times no matter how many times I remove it, it still fires. Any ideas on what could be causing this?
Edit
The code is in a Form that has a virtual mode datagridview. I want to run some operations that will trigger the CellValueNeeded event for the datagridview without that event being fired (because it will interfere).
Public Sub DoEventRaisingStuff()
RemoveHandler grid.CellValueNeeded, AddressOf grid_CellValueNeeded
'Do things that would trigger CellValueNeeded
AddHandler grid.CellValueNeeded, AddressOf grid_CellValueNeeded
End Sub
Removing the handler multiple times does not prevent the event from firing, so it doesn't seem to be added multiple times somewhere else by accident.
Is there a way to find out what event handlers are active?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果正在调用事件处理代码,则会发生以下两种情况之一:
您没有删除事件处理程序。
您正在多次添加事件处理程序。这是更常见的情况。
过去,我能够发现 2. 的唯一方法是找到添加事件处理程序的所有位置(希望只有一两个)并在这些行上放置断点。然后,我在调试器下运行该应用程序,发现它中断的次数比我预期的要多。我使用调用堆栈来找出原因 - 我总是将添加代码放在错误的位置(例如,在按下按钮时而不是在表单实例化时)。
您可以对删除代码执行相同的操作。计算每个断点被击中的次数,如果它们不相同,请备份调用堆栈,看看是否可以找出原因。
If the event handling code is being called then one of two things is happening:
You aren't removing the event handler.
You are adding the event handler multiple times. This is the more usual case.
In the past the only way I've been able to spot 2. is to find all the places where the event handler is added (hopefully only one or two) and put break points on those lines. I've then run the application under the debugger and found that it breaks more times than I expect. I use the call stack to work out why - it's always me putting the add code in the wrong place (on a button press rather than on form instantiation for example).
You can do the same with the removal code. Count the number of times each break point is hit and if they're not the same work back up the call stack to see if you can work out why.
在函数中使用类作用域标志并在事件处理程序中检查该标志。
IE:
Use class scoped flag in the function and check the flag in the event handler.
i.e.: