ADDHANDLER

Register a method as an event handler for an event

WTSupported in traditional Synergy on Windows
WNSupported in Synergy .NET on Windows

 

 

ADDHANDLER(event, method_name)

or

event += method_name

Arguments

event

The name of an event, or an object reference to a field that is of the type Synnet.DotNetEvent or inherited from Synnet.DotNetEvent.

method_name

The name of the method (including the instance) that will handle the event.

Discussion

The ADDHANDLER statement registers an event handler to an accessible event from an instantiated .NET class. In traditional Synergy, ADDHANDLER can only be used with code generated by the gennet40 utility.

Method_name is in the form instance.method. For example, if c1 is an instance of class1, method_name could be any of the following:

When registering delegates with ADDHANDLER, you are allowed to use generated classes as arguments, which allows a method like the following to be called as a delegate

method meth1            ;Definition of method registered with the delegate
    p1          ,@System.Object
    p2          ,@System.EventArgs
proc

.
.
.
endmethod

You can register more than one event handler for an accessible event using ADDHANDLER. You can also register an event handler for an event more than one time. If an event handler is registered multiple times, the event handler will be called once for each registration when the event is fired.

See also

Examples

The example below defines a new .NET class that contains a delegate, an event, and a method to register for that event. (Note that the signature of this method must match that of the delegate.) The method is registered as an event handler for the event. The RAISEEVENT statement causes all registered event handlers to be executed, and parameter values passed to RAISEEVENT are in turn passed to the registered event handlers. When the event goes out of scope, all methods registered for the event are automatically unregistered.

class class1
    public delegate mydelegate, void
        msg, a
    enddelegate
    public event myevent, @mydelegate
    public method mymethod, void
        msg, a
    proc
        open(2,o,"tt:")
          writes(2,"in mymethod "+msg)
          close(2)
          mreturn
    endmethod
endclass
c1, @class1
c1 = new class1()
addhandler(c1.myevent, c1.mymethod)
raiseevent(c1.myevent, "pass this message")
removehandler(c1.myevent, c1.mymethod)