gpt4 book ai didi

vb.net - 如何删除从类 VB.NET 的实例中添加的包装/自定义事件处理程序

转载 作者:行者123 更新时间:2023-12-01 14:35:31 26 4
gpt4 key购买 nike

我需要创建 zkemkeeper.CZKEM 的多个实例一次创建与多个具有事件的生物识别设备的事件(250+)连接。我创建了一个类来完成我想要的输出:

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将自定义设备名称嵌入到每个生物识别设备中,以便我可以识别哪个设备 [如 CZKEM2 ] 引发任何事件 [如 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 无法回答,是否还有其他 [工作] 满足我的要求的选项?

我现在在这里被困了一个星期,我在谷歌上找不到与我的问题类似的任何东西。

请帮助我任何人:(

最佳答案

当您要删除事件时,这是您当前正在执行的操作:

Public Sub RemoveBioHandler(iDevice As String, CZKEM As zkemkeeper.CZKEM)
MyNewWrapperClass = New MyWrapperClass(iDevice, CZKEM)
RemoveHandler MyNewWrapperClass.AttEventWrapper, AddressOf LogRaised
End Sub

您正在创建 新品 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

关于vb.net - 如何删除从类 VB.NET 的实例中添加的包装/自定义事件处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55897805/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com