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

时间:2019-04-29 06:08:02

标签: vb.net multiple-instances eventhandler biometrics zkemkeeper

我需要创建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类的另一个实例创建事件处理程序,或者将所有事件处理程序删除?

问题2 如果问题#1无法回答,我是否还有其他 [工作] 选项可以满足我的要求?

我现在在这里呆了一个星期,我在Google找不到像我的问题一样的东西。

请帮助我任何人:(

1 个答案:

答案 0 :(得分:1)

这是您要删除事件时当前正在做的事情:

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