MATLAB Object-Oriented Programming

(Joyce) #1

Listener Lifecycle


In this section...
“Control Listener Lifecycle” on page 11-28
“Temporarily Deactivate Listeners” on page 11-28
“Permanently Delete Listeners” on page 11-28

Control Listener Lifecycle


There are two ways to create listeners:


  • handle.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.

  • handle.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” on page 11-22.

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:

11 Events — Sending and Responding to Messages

Free download pdf