Products & Services Industries Academia Support User Community Company

Learn more about MATLAB   

Callback Interruption

Callback Execution

Callback execution is event driven and callbacks from different GUIs share the same event queue. In general, callbacks are triggered by user events such as a mouse click or key press. Because of this, you cannot predict, when a callback is requested, whether or not another callback is executing or, if one is, which callback it is.

If a callback is executing and the user triggers an event for which a callback is defined, that callback attempts to interrupt the callback that is already executing. When this occurs, MATLAB software processes the callbacks according to these factors:

How the Interruptible Property Works

An object's Interruptible property can be either on (the default) or off.

If theInterruptible property of the object whose callback is executing is on, the callback can be interrupted. However, it is interrupted only when it, or a function it triggers, calls drawnow, figure, getframe, pause, or waitfor. Before performing their defined tasks, these functions process any events in the event queue, including any waiting callbacks. If the executing callback, or a function it triggers, calls none of these functions, it cannot be interrupted regardless of the value of its object's Interruptible property.

If the Interruptible property of the object whose callback is executing is off, the callback cannot be interrupted with the following exceptions. If the interrupting callback is a DeleteFcn or CreateFcn callback or a figure's CloseRequest or ResizeFcn callback, it interrupts an executing callback regardless of the value of the executing callback object's Interruptible property. These callbacks too can interrupt only when a drawnow, figure, getframe, pause, or waitfor function executes.

The callback properties to which Interruptible can apply depend on the objects for which the callback properties are defined:

To prevent continuously repeating callbacks such as the above from being interrupted, set the value of the Interruptible property of the object whose callback is repeating to 'off':

set(hObject,'Interruptible','off');

where hObject is the handle to the object whose callback is called continuously (for example, to load another GUIDE GUI).

How the Busy Action Property Works

An object's BusyAction property can be either queue (the default) or cancel. The BusyAction property of the interrupting callback's object is taken into account only if the Interruptible property of the executing callback's object is off, i.e., the executing callback is not interruptible.

If a noninterruptible callback is executing and an event (such as a mouse click) triggers a new callback, MATLAB software uses the value of the new callback object's BusyAction property to decide whether to queue the requested callback or cancel it.

If an interruptible callback is executing, the requested callback runs when the executing callback terminates or calls drawnow, figure, getframe, pause, or waitfor. The BusyAction property of the requested callback's object has no effect.

Example

This example demonstrates control of callback interruption using the Interruptible and BusyAction properties. It creates two GUIs:

Using the Example GUIs

Click here to run the example GUIs.

To see the interplay of the Interruptible and BusyAction properties:

  1. Click one of the Wait buttons in the first GUI. Both buttons create and update a waitbar.

  2. While the waitbar is active, click either the Surf Plot or the Mesh Plot button in the second GUI. The Surf Plot button creates a surf plot using peaks data. The Mesh Plot button creates a mesh plot using the same data.

The following topics describe what happens when you click specific combinations of buttons:

Clicking a Wait Button.  

The Wait buttons are the same except for their Interruptible properties. Their Callback callbacks, which are essentially the same, call the utility function create_update_waitbar which calls waitbar to create and update a waitbar. The Wait (Interruptible) button Callback callback,wait_interruptible, can be interrupted each time waitbar calls drawnow. The Wait (Noninterruptible) button Callback callback,wait_noninterruptible, cannot be interrupted (except by specific callbacks listed in How the Interruptible Property Works).

This is the Wait (Interruptible) button Callback callback,wait_interruptible:

    function wait_interruptible(hObject,eventdata)
        % Disable the other push button.
        set(h_noninterrupt,'Enable','off')
        % Clear the axes in the other GUI.
        cla(h_axes2,'reset')
        % Create and update the waitbar.
        create_update_waitbar
        % Enable the other push button
        set(h_noninterrupt,'Enable','on')
    end

The callback first disables the other push button and clears the axes in the second GUI. It then calls the utility function create_update_waitbar to create and update a waitbar. When create_update_waitbar returns, it enables the other button.

Clicking a Plot Button.   What happens when you click a Plot button depends on which Wait button you clicked first and the BusyAction property of the Plot button.

View the Complete GUI M-File

If you are reading this in the MATLAB Help browser, you can click here to display a complete listing of the code used in this example in the MATLAB Editor.

  


Recommended Products

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.

 © 1984-2009- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS