<< Prev        Next >>

ID : 3903

AddHandler

Function

Enter a method to receive an event from the specified provider object. Once an event is issued by the provider, the entered method is called.

Syntax

AddHandler object, event name, method to receive

Guaranteed Entry

Object
Specify an object to issue events. Use an object obtained by AddController.
Event name
Specify an event name to receive in string type data. Use "OnMessage" normally.
Method to receive events
Specify a method to receive events.

Description

Enter a method to receive an event from the specified provider object. Once an event is issued by the provider, the entered method is called.

This method needs to be the format below.

Sub method name (ByVal Sender As Object, ByVal Args As Variant)
  '...
End Sub
To enhance readability, using the same method name as a event name is recommended although the method name can be designated in arbitrary. Other two types are fixed. Be sure to follow the format above. Usually, specify "OnMessage" as a method name, and write a method as shown below.
Sub OnMessage (ByVal Sender As Object, ByVal Args As Variant)
'...
End Sub

Soon after the event receiving becomes unnecessary, call RemoveHandler to end the procedure.

Attention

  • To receive the events, a task which runs AddHandler needs to keep running. If the task stops, events cannot be received.
  • Wait command cannot be used to keep command waiting to receive the events. Use loop codes which includes Delay command instead.

Example

Sample code shows how to receive a reading data with the QR code provider when the QR code reader reads QR code.

Dim g_Counter As Integer = 0 'Number of data to receive

Sub Main()
Dim ctrl As Object
ctrl = Cao.AddController( "QR", "CaoProv.DENSO.QRCode", "",
      "Conn=com:6:38400:N:8:1, Mode=5, Protocol=0:0:0" )

'Specify the event name and area to receive
AddHandler ctrl, "OnMessage", OnMessage 
g_Counter = 0

'Loop command to keep waiting event receiving
Do
Delay 10
If g_Counter >= 3 Then Exit Do
Loop 

 'Stop event receiving
RemoveHandler ctrl

End Sub

'Event Handler for OnMessage
Sub OnMessage( ByVal Sender As Object, ByVal Args As Variant )
Dim msg as object
msg = Args(0) '::= CaoMessage object
PrintDbg "" & Time & " - CODE = [" & msg.Value & "]"
g_Counter = g_Counter + 1
End Sub

ID : 3903

<< Prev        Next >>