vb.netevent-handlingaddhandlerraiseevent

How to "AddHandler" trying to catch an event from an external library


I want to call a function as soon as an event from an external library occurs.

I have an array of camera objects (different cameras) which create an event as soon as they grab an image (triggered externally). The cameras are defined by an external Library (Basler) and were defined in the code before so the event is defined as follows:

cameras(i).StreamGrabber.ImageGrabbed

I created a sub

Sub ImageGrabEvent(sender As Object, e As EventArgs)
    MsgBox("Aha")
End Sub</code>

and tried to register in the main part with

AddHandler cameras(i).StreamGrabber.ImageGrabbed, AddressOf (ImageGrabEvent)

and also tried

AddHandler cameras(i).StreamGrabber.ImageGrabbed, ImageGrabEvent()

and variations with "new" or whatever.

Extra Challenge: Any camera can raise the event, how can I identify in my sub which camera did it?


Solution

  • You should not have brackets (parentheses) around the handler method so this:

    AddHandler cameras(i).StreamGrabber.ImageGrabbed, AddressOf (ImageGrabEvent)
    

    should be:

    AddHandler cameras(i).StreamGrabber.ImageGrabbed, AddressOf ImageGrabEvent
    

    The sender object gives you the object that raised the event