如何删除从 class VB.NET 的实例添加的 wrapped/custom 事件处理程序
How to remove wrapped/custom event handler added from an instance of a class VB.NET
我需要创建 zkemkeeper.CZKEM
的多个实例,以便一次创建与多个生物识别设备的活动 (250+) 连接。我创建了一个 class 来执行我想要的输出:
Public Class ZKEMEventsClass
Public CZKEM2 As New zkemkeeper.CZKEM
Public MyNewWrapperClass As MyWrapperClass
Public Sub AddBioHandler(iDevice As String, iIP As String, iPort As Integer)
If CZKEM2.Connect_Net(iIP, iPort) Then
If CZKEM2.RegEvent(1, 65535) = True Then
MyNewWrapperClass = New MyWrapperClass(iDevice, CZKEM2)
AddHandler MyNewWrapperClass.AttEventWrapper, AddressOf LogRaised
MsgBox("Handler successfully registered")
Else
MsgBox("Error Registering Events")
End If
Else
MsgBox("Error Connecting to Device")
End If
End Sub
Public Sub RemoveBioHandler(iDevice As String, CZKEM As zkemkeeper.CZKEM)
>>> MyNewWrapperClass = New MyWrapperClass(iDevice, CZKEM)
>>> RemoveHandler MyNewWrapperClass.AttEventWrapper, AddressOf LogRaised
End Sub
Public Sub LogRaised(ByVal SenderName As String, ByVal sEnrollNumber As String, ByVal iIsInValid As Integer, ByVal iAttState As Integer, ByVal iVerifyMethod As Integer, ByVal iYear As Integer, ByVal iMonth As Integer, ByVal iDay As Integer, ByVal iHour As Integer, ByVal iMinute As Integer, ByVal iSecond As Integer, ByVal iWorkCode As Integer)
MsgBox("Raised event details here... [EnrollID, Year, Month, Day]...")
End Sub
End Class
注意: 我创建并使用 MyWrapperClass
将自定义设备名称嵌入到每个生物识别设备中,以便我可以识别哪个设备 [like CZKEM2
] 引发了任何事件 [like CZKEM2.OnAttTransactionEx
]
Public Class MyWrapperClass
Public Property Name
Private WithEvents CZKEM As zkemkeeper.CZKEM
Public Event AttEventWrapper(SenderName As String, sEnrollNumber As String, iIsInValid As Integer, iAttState As Integer, iVerifyMethod As Integer, iYear As Integer, iMonth As Integer, iDay As Integer, iHour As Integer, iMinute As Integer, iSecond As Integer, iWorkcode As Integer)
Public Sub New(WrapperName As String, CZKEMObject As zkemkeeper.CZKEM)
Me.Name = WrapperName
Me.CZKEM = CZKEMObject
End Sub
Private Sub HandleEvent(ByVal sEnrollNumber As String, ByVal iIsInValid As Integer, ByVal iAttState As Integer, ByVal iVerifyMethod As Integer, ByVal iYear As Integer, ByVal iMonth As Integer, ByVal iDay As Integer, ByVal iHour As Integer, ByVal iMinute As Integer, ByVal iSecond As Integer, ByVal iWorkCode As Integer) Handles CZKEM.OnAttTransactionEx
RaiseEvent AttEventWrapper(Me.Name, sEnrollNumber, iIsInValid, iAttState, iVerifyMethod, iYear, iMonth, iDay, iHour, iMinute, iSecond, iWorkCode)
End Sub
End Class
在我的主程序中,我使用了以下代码:
Sub ConnectToDevice()
Dim iIP As String
Dim iDevice As String
Dim iPort As Integer
For x = 1 To 2
Select Case x
Case 1
iIP = "122.3.47.43"
iDevice = "Device 1"
Case 2
iIP = "192.168.10.201"
iDevice = "Device 2"
End Select
'This is the section where I create new instance of my ZKEMEventsClass
Dim NewConnect As New ZKEMEventsClass
NewConnect.AddBioHandler(iDevice, iIP, iPort)
Next
End Sub
Sub Disconnect()
>>> For Each CZKEMObject As KeyValuePair(Of String, zkemkeeper.CZKEM) In MyWrapperClass.ListOfDevices
>>> Dim NewRemoveHandler As New ZKEMEventsClass
>>> NewRemoveHandler.RemoveBioHandler(CZKEMObject.Key, CZKEMObject.Value)
>>> Next
End Sub
问题 #1:
我如何删除我的每个事件处理程序,或者更确切地说,当它们都是从我的 ZKEMEventsClass
class 的另一个实例创建的时?
问题#2
如果问题 #1 无法回答,是否还有其他 [working] 选项可以满足我的要求?
我在这里被困了一个星期了,我在 google 中找不到任何与我的问题类似的东西。
请任何人帮助我:(
当您要删除事件时,这就是您当前正在做的事情:
Public Sub RemoveBioHandler(iDevice As String, CZKEM As zkemkeeper.CZKEM)
MyNewWrapperClass = New MyWrapperClass(iDevice, CZKEM)
RemoveHandler MyNewWrapperClass.AttEventWrapper, AddressOf LogRaised
End Sub
您正在创建一个 new ZkemEventClass 对象(它将处理程序绑定到它的事件,因为您已经在构造函数中定义了它)然后调用 removeBioHandler
在这个新创建的对象上,它成功地删除了处理程序。但是您从未使用过该对象,该对象不是您之前创建并正在使用的对象。它仅存在于您定义它的子目录中。
在您的主程序中,您需要保留对您拥有的每个 ZKemEventClass 对象的引用,当需要摆脱它时,您将其称为 RemoveBioHandler 属性。这里的关键是您为 同一对象 调用方法。不是一些新创建的。
至于保留对包装类的引用字典,您可以这样做:
Public Class MyManagerClass
Public MyDevicesDictionary As New Dictionary(Of String, MyWrapperClass)
Public Sub AddDevice(Device As MyWrapperClass)
MyDevicesDictionary.Add(Device.Name, Device)
AddHandler Device.AttEventWrapper, AddressOf EventHandlerName
End Sub
Public Sub RemoveDevice(DeviceName As String)
Dim Device As MyWrapperClass = MyDevicesDictionary(DeviceName)
MyDevicesDictionary.Remove(DeviceName)
RemoveHandler Device.AttEventWrapper, AddressOf EventHandlerName
End Sub
Public Sub EventHandlerName(Name, sEnrollNumber, iIsInValid, iAttState, iVerifyMethod, iYear, iMonth, iDay, iHour, iMinute, iSecond, iWorkCode)
'do whatever you want to do here
End Sub
End Class
我需要创建 zkemkeeper.CZKEM
的多个实例,以便一次创建与多个生物识别设备的活动 (250+) 连接。我创建了一个 class 来执行我想要的输出:
Public Class ZKEMEventsClass
Public CZKEM2 As New zkemkeeper.CZKEM
Public MyNewWrapperClass As MyWrapperClass
Public Sub AddBioHandler(iDevice As String, iIP As String, iPort As Integer)
If CZKEM2.Connect_Net(iIP, iPort) Then
If CZKEM2.RegEvent(1, 65535) = True Then
MyNewWrapperClass = New MyWrapperClass(iDevice, CZKEM2)
AddHandler MyNewWrapperClass.AttEventWrapper, AddressOf LogRaised
MsgBox("Handler successfully registered")
Else
MsgBox("Error Registering Events")
End If
Else
MsgBox("Error Connecting to Device")
End If
End Sub
Public Sub RemoveBioHandler(iDevice As String, CZKEM As zkemkeeper.CZKEM)
>>> MyNewWrapperClass = New MyWrapperClass(iDevice, CZKEM)
>>> RemoveHandler MyNewWrapperClass.AttEventWrapper, AddressOf LogRaised
End Sub
Public Sub LogRaised(ByVal SenderName As String, ByVal sEnrollNumber As String, ByVal iIsInValid As Integer, ByVal iAttState As Integer, ByVal iVerifyMethod As Integer, ByVal iYear As Integer, ByVal iMonth As Integer, ByVal iDay As Integer, ByVal iHour As Integer, ByVal iMinute As Integer, ByVal iSecond As Integer, ByVal iWorkCode As Integer)
MsgBox("Raised event details here... [EnrollID, Year, Month, Day]...")
End Sub
End Class
注意: 我创建并使用 MyWrapperClass
将自定义设备名称嵌入到每个生物识别设备中,以便我可以识别哪个设备 [like CZKEM2
] 引发了任何事件 [like CZKEM2.OnAttTransactionEx
]
Public Class MyWrapperClass
Public Property Name
Private WithEvents CZKEM As zkemkeeper.CZKEM
Public Event AttEventWrapper(SenderName As String, sEnrollNumber As String, iIsInValid As Integer, iAttState As Integer, iVerifyMethod As Integer, iYear As Integer, iMonth As Integer, iDay As Integer, iHour As Integer, iMinute As Integer, iSecond As Integer, iWorkcode As Integer)
Public Sub New(WrapperName As String, CZKEMObject As zkemkeeper.CZKEM)
Me.Name = WrapperName
Me.CZKEM = CZKEMObject
End Sub
Private Sub HandleEvent(ByVal sEnrollNumber As String, ByVal iIsInValid As Integer, ByVal iAttState As Integer, ByVal iVerifyMethod As Integer, ByVal iYear As Integer, ByVal iMonth As Integer, ByVal iDay As Integer, ByVal iHour As Integer, ByVal iMinute As Integer, ByVal iSecond As Integer, ByVal iWorkCode As Integer) Handles CZKEM.OnAttTransactionEx
RaiseEvent AttEventWrapper(Me.Name, sEnrollNumber, iIsInValid, iAttState, iVerifyMethod, iYear, iMonth, iDay, iHour, iMinute, iSecond, iWorkCode)
End Sub
End Class
在我的主程序中,我使用了以下代码:
Sub ConnectToDevice()
Dim iIP As String
Dim iDevice As String
Dim iPort As Integer
For x = 1 To 2
Select Case x
Case 1
iIP = "122.3.47.43"
iDevice = "Device 1"
Case 2
iIP = "192.168.10.201"
iDevice = "Device 2"
End Select
'This is the section where I create new instance of my ZKEMEventsClass
Dim NewConnect As New ZKEMEventsClass
NewConnect.AddBioHandler(iDevice, iIP, iPort)
Next
End Sub
Sub Disconnect()
>>> For Each CZKEMObject As KeyValuePair(Of String, zkemkeeper.CZKEM) In MyWrapperClass.ListOfDevices
>>> Dim NewRemoveHandler As New ZKEMEventsClass
>>> NewRemoveHandler.RemoveBioHandler(CZKEMObject.Key, CZKEMObject.Value)
>>> Next
End Sub
问题 #1:
我如何删除我的每个事件处理程序,或者更确切地说,当它们都是从我的 ZKEMEventsClass
class 的另一个实例创建的时?
问题#2 如果问题 #1 无法回答,是否还有其他 [working] 选项可以满足我的要求?
我在这里被困了一个星期了,我在 google 中找不到任何与我的问题类似的东西。
请任何人帮助我:(
当您要删除事件时,这就是您当前正在做的事情:
Public Sub RemoveBioHandler(iDevice As String, CZKEM As zkemkeeper.CZKEM)
MyNewWrapperClass = New MyWrapperClass(iDevice, CZKEM)
RemoveHandler MyNewWrapperClass.AttEventWrapper, AddressOf LogRaised
End Sub
您正在创建一个 new ZkemEventClass 对象(它将处理程序绑定到它的事件,因为您已经在构造函数中定义了它)然后调用 removeBioHandler
在这个新创建的对象上,它成功地删除了处理程序。但是您从未使用过该对象,该对象不是您之前创建并正在使用的对象。它仅存在于您定义它的子目录中。
在您的主程序中,您需要保留对您拥有的每个 ZKemEventClass 对象的引用,当需要摆脱它时,您将其称为 RemoveBioHandler 属性。这里的关键是您为 同一对象 调用方法。不是一些新创建的。
至于保留对包装类的引用字典,您可以这样做:
Public Class MyManagerClass
Public MyDevicesDictionary As New Dictionary(Of String, MyWrapperClass)
Public Sub AddDevice(Device As MyWrapperClass)
MyDevicesDictionary.Add(Device.Name, Device)
AddHandler Device.AttEventWrapper, AddressOf EventHandlerName
End Sub
Public Sub RemoveDevice(DeviceName As String)
Dim Device As MyWrapperClass = MyDevicesDictionary(DeviceName)
MyDevicesDictionary.Remove(DeviceName)
RemoveHandler Device.AttEventWrapper, AddressOf EventHandlerName
End Sub
Public Sub EventHandlerName(Name, sEnrollNumber, iIsInValid, iAttState, iVerifyMethod, iYear, iMonth, iDay, iHour, iMinute, iSecond, iWorkCode)
'do whatever you want to do here
End Sub
End Class