本文介绍了WithEvents(Handles)与AddHandler之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我搜索了关于使用关键字句柄而不是 AddHandler ,在 VB.NET ,但我无法解释为什么这段代码不起作用。I've searched for the difference about the use of the keyword Handles instead of AddHandler, in VB.NET, but I'm unable to explain why this code doesn't work..Imports System.ThreadingPublic Class MyClass_EventArgs Inherits System.EventArgsEnd ClassPublic Class MyClass Public Event MainThreadFinished(ByVal sender As Object, ByVal e As MyClass_EventArgs) Private WithEvents MyEvents As MyClass Private trd As Thread Public Sub New() 'AddHandler MainThreadFinished, AddressOf Me.MyEvents_ThreadFinished trd = New Thread(AddressOf mainThread) trd.IsBackground = True trd.Start() RaiseEvent MainThreadFinished(Me, Nothing) End Sub Protected Overrides Sub Finalize() trd.Abort() End Sub Protected Sub MyEvents_ThreadFinished(ByVal sender As Object, ByVal e As MyClass_EventArgs) _ Handles MyEvents.MainThreadFinished MessageBox.Show("AAA") End Sub Private Sub mainThread() RaiseEvent MainThreadFinished(Me, Nothing) End SubEnd Class对于事件,但如果我取消注释后续行,代码工作和消息框出现... Well, this code never respond to the events, but if I uncomment the followin line, the code works and the messagebox appear...'AddHandler MainThreadFinished, AddressOf Me.MyEvents_ThreadFinished为什么会这样?推荐答案看起来你已经做了一个很好的发现!根据Microsoft文档, RaiseEvent Statement ,It looks like you've made a fine discovery! Per Microsoft documentation, RaiseEvent Statement, 不应在类的构造函数中引发非共享事件被宣布。虽然这样的事件不会导致运行时错误,但它们可能无法被关联的事件处理程序捕获。如果您需要从构造函数引发事件,请使用共享修饰符创建共享事件。 Non-shared events should not be raised within the constructor of the class in which they are declared. Although such events do not cause run-time errors, they may fail to be caught by associated event handlers. Use the Shared modifier to create a shared event if you need to raise an event from a constructor.换句话说,微软说你不应该做你正在做的事情,如果你必须使用共享的事件。In other words, Microsoft says you shouldn't be doing what you're doing - and if you must, to use shared events.在查看其他来源时,我会说, AddHandler 和 Handles 之间的区别是句法的问题糖。您可能需要查看C#中的事件如何获得更多的洞察力(例如在 C#事件 )。 句柄与 WithEvents 一起使用,作为类的一个实例自动订阅事件(这是另外在C#中使用 + = 和VB.NET中的 AddHander 明确完成。In looking through other sources, I would say that the difference between AddHandler and Handles is a matter of syntactic sugar. You may want to look into how events are done in C# for more insight (such as in C# Events). Handles is used in conjunction with WithEvents as a way for an instance of a class to automatically subscribe to events (which is otherwise explicitly done with += in C# and with AddHander in VB.NET).看来,您的显式 AddHandler 确保事件联接位于 RaiseEvent ,所以它可以按照你想要的方式工作。我只能猜测,没有这些事件联接还没有完成 - 也就是说,它没有工作,因为编译器插入的代码执行相当于AddHandler幕后,任何设计模式的编译器编写器视为适当。鉴于他们对此的警告,设计师似乎很清楚这一可能的后果。It would seem that your explicit AddHandler ensures that the event hookups are in place before the RaiseEvent, and so then it works as you wanted. I can only guess that without that, those event hookups weren't yet done - that is, it didn't work because of however the compiler inserts the code that performs the equivalent of AddHandler behind the scenes, by whatever design pattern the compiler writers deemed as appropriate. It would seem that the designers were well aware of this possible consequence, given their warning about this. 这篇关于WithEvents(Handles)与AddHandler之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-18 11:34