%% Control Callback Interruption Behavior
% This example shows how the Interruptible and BusyAction component properties interact to produce different types of callback interruption behavior.
% Create a file called callbackBehavior.m in your current folder, and define in it a function with the same name. This function creates an app with two figure windows, each with two buttons. Each of the buttons has a ButtonPushedFcn callback and a different callback execution property value. If you click one button, and then click a second button before the first one is done, then the callback of the second button attempts to interrupt the first. The buttons in the first window display and update a progress dialog when clicked. The buttons in the second window plot data when clicked. You can control what happens by defining the interruption behavior for the two buttons.
% function callbackBehavior
% % Create the figures and grid layouts
% fig1 = uifigure('Position',[400 600 500 150]);
% g1 = uigridlayout(fig1,[2,2]);
% fig2 = uifigure('Position',[400 100 500 400]);
% g2 = uigridlayout(fig2,[3,2], ...
%     'RowHeight', {'1x','1x','8x'});
%
% % Create the label for the first figure window
% lbl1 = uilabel(g1,'Text','1. Click a button to clear the axes and generate a progress dialog.');
% lbl1.Layout.Column = [1 2];
% lbl1.HorizontalAlignment = 'center';
%
% % Create the buttons that create a progress dialog
% interrupt = uibutton(g1, ...
%     'Text','Wait (interruptible)', ...
%     'Interruptible','on', ...
%     'ButtonPushedFcn',@createProgressDlg);
% nointerrupt = uibutton(g1, ...
%     'Text','Wait (not interruptible)', ...
%     'Interruptible','off', ...
%     'ButtonPushedFcn',@createProgressDlg);
%
% % Create the label for the second figure window
% lbl2 = uilabel(g2,'Text','2. Click a button to plot some data.');
% lbl2.Layout.Column = [1 2];
% lbl2.HorizontalAlignment = 'center';
%
% % Create the axes
% ax = uiaxes(g2);
% ax.Layout.Row = 3;
% ax.Layout.Column = [1 2];
%
% % Create the buttons to plot data
% queue = uibutton(g2, ...
%     'Text','Plot (queue)', ...
%     'BusyAction','queue', ...
%     'ButtonPushedFcn',@(src,event)surf(ax,peaks(35)));
% queue.Layout.Row = 2;
% queue.Layout.Column = 1;
%
% cancel = uibutton(g2, ...
%     'Text','Plot (cancel)', ...
%     'BusyAction','cancel', ...
%     'ButtonPushedFcn',@(src,event)surf(ax,peaks(35)));
% cancel.Layout.Row = 2;
% cancel.Layout.Column = 2;
%
%     % Callback function to create and update a progress dialog
%     function createProgressDlg(src,event)
%         % Clear axes
%         cla(ax,'reset')
%         % Create the dialog
%         dlg = uiprogressdlg(fig1,'Title','Please Wait',...
%         'Message','Loading...');
%         steps = 250;
%         for step = 1:steps
%             % Update progress
%             dlg.Value = step/steps;
%             pause(0.01)
%         end
%         close(dlg)
%     end
% end
% Call the callbackBehavior function to display the figure windows.

callbackBehavior

% Click pairs of buttons to see the effects of different combinations of Interruptible and BusyAction property values.
% Callback interruption — Click Wait (interruptible) immediately followed by either button in the second window: Plot (queue) or Plot (cancel). Because the first button has its Interruptible value set to 'on', interruption occurs. The plot appears while the progress dialog is still running.
% Callback queueing — Click Wait (not interruptible) immediately followed by Plot (queue). Because the first button has its Interruptible value set to 'on' and the second button has its BusyAction value set to 'queue', queueing occurs. The progress dialog runs to completion. Then, the plot displays.
% Callback cancellation — Click Wait (not interruptible) immediately followed by Plot (cancel). Because the first button has its Interruptible value set to 'on' and the second button has its BusyAction value set to 'cancel', cancellation occurs. The progress dialog runs to completion. But then, no plot appears, because MATLAB® has discarded the plot callback.
% Copyright 2020 The MathWorks, Inc.