Subscribe methods to events

Introduction

In C#, the association between a method and an event, or the execution of the method each time the event is generated, is called “subscription”.

When an object generates an event, the event data are displayed using a corresponding delegate method called event handler, to which a method can be subscribed.

In particular, it is possible to subscribe methods to the following events:

  • change value of a variable

  • all events generated by an object

  • a specific event generated by an object

For references on event handlers, see Variables and objects generic events and Object and variable references.

Syntax for subscription

Subscription is typically made inside the Start() method, as shown in the example below:

public override void Start()
{
    Session.UserChange += Session_UserChange;
}

In the example, using the UserChange event handler and the += operator, the Session_UserChange method is subscribed to the user change event.

Warning

Always cancel the subscription in the Stop() method to avoid a memory leak.

Hint

in Visual Studio, after the += operator, press the TAB key two times to automatically insert a new method. To automatically insert the method in Visual Studio Code, refer to the editor’s suggestions.

To define the method to be executed, refer to the following example:

private void Session_UserChange(object sender, UserChangeEventArgs e)
{
    Log.Info(e.newUser.BrowseName);
}

The method arguments are the following:

  • sender: IUAObject object corresponding to the node origin of the event

  • e: C# object that contains the event data, different based on the type of data of the event handler (in the example, the data type is UserChangeEventArgs).

Syntax to cancel the subscription

Warning

Always cancel the subscription in the Stop() method to avoid a memory leak.

To cancel a subscription, use the -= operator. Refer to the example below:

public override void Stop()
{
    Session.UserChange -= Session_UserChange;
}

Subscribe a method to the value change of a variable event

To subscribe a method to the value change of a variable, use the VariableChange event handler, supplied by the IUAVariable class (see Variables and objects generic events).

Subscribe a method to all the events generated by an object

To subscribe a method to all the events generated by an object, use the UAEvent event handler, supplied by the IUAObject class (see Variables and objects generic events).

Subscribe a method to a specific event of an object

To subscribe a method to a specific event generated by an object, use the corresponding event handler supplied by the type (for references on type event handlers, see Object and variable references).

Below is an example in which the OnMouseClick event handler is used, supplied by the Button class, to execute the Button2_OnMouseClick method each time Button2 is pressed.

public override void Start()
{
    var button2 = Owner.Get<Button>("Button2");
    button2.OnMouseClick += Button2_OnMouseClick;
}

private void Button2_OnMouseClick(object sender, MouseClickEvent e)
{
    var label2 = Owner.Get<Label>("Label2");
    var button = (Button)sender;
    label2.Text = "Mouse click event on " + button.BrowseName;
}