Main Content

Listener Lifecycle

Control Listener Lifecycle

There are two ways to create listeners:

  • addlistener creates a coupling between the listener and event source object. The listener object persists until you delete it or until the event object is destroyed. When the event source object is destroyed, MATLAB® automatically destroys the listener object.

  • listener constructs listener objects that are not coupled to the lifecycle of the event source object. The listener is active as long as the listener object remains in scope and is not explicitly deleted. Therefore, your application must maintain a reference to the listener object by storing the listener handle. The advantage of uncoupling the listener and event objects is that you can define and destroy each independently.

For more information, see Events and Listeners Syntax.

Temporarily Deactivate Listeners

The addlistener method returns the listener object so that you can set its properties. For example, you can temporarily disable a listener by setting its Enabled property to false:

ListenerHandle.Enabled = false;

To reenable the listener, set Enabled to true.

ListenerHandle.Enabled = true;

Permanently Delete Listeners

Calling delete on a listener object destroys it and permanently removes the listener:


Related Topics