vb.netmultiple-instanceseventhandlerbiometricszkemkeeper

How to remove wrapped/custom event handler added from an instance of a class VB.NET


I need to create multiple instance of zkemkeeper.CZKEM to create active (250+) connections to multiple biometrics devices with events at a time. I created a class to do my desired output:

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

NOTE: I created and used MyWrapperClass to embed custom device name into each biometrics device so I can identify which device [like CZKEM2] raised whatever event [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

In my main program, I used codes below:

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

QUESTION #1: How can I remove each of my event handler or rather all when it was all created from another instance of my ZKEMEventsClass class?

QUESTION #2 If question #1 is not answerable, are there other [working] options for me to meet my requirements?

I'm kind of stucked here for a week now, and I can't find anything from google similar to my problem.

Please help me anyone :(


Solution

  • This is what you are currently doing when you want to remove an event:

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

    You are creating a new ZkemEventClass Object (which ties a handler to it's event because you've defined it so in the constructor) and then you call removeBioHandleron this newly created object, which succesfully removes the handler. But you've never used this object, this object is not the object you created earlier and were working with. it exist only within the sub where you defined it.

    In your main program you need to hold on to a reference of every ZKemEventClass Object you have, and when the time comes to get rid of it, you call it's RemoveBioHandler Property. The Key thing here is you call the method for that same object. Not some newly created one.

    As For keeping a Dictionary of references to you wrapperclasses, you could do something like this :

    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