Cancel parfeval with a uiprogressdlg
Show older comments
Hello,
I'm making a UI that works in parallel and a uiprogressdlg is running while it's not finished.
I'm trying to make the cancel button of this uiprogressdlg do something but it's like nothing is happening when I push it.
I tried to put a line in me second loop above. It does nothing since when I push the button the code is doing the fetchNext line and when the fetchNext is done, every job are done...
I also add a listener to the button, but nothing happens when I push the cancel button...
Any hint how to stop all working jobs by hitting this cancel button ?
function testCancel()
clearvars
close all
clc
figT = uifigure('MenuBar','none','toolbar','none','NumberTitle','off','Name','main window');
uibutton(figT, 'push', 'Text', 'run', 'ButtonPushedFcn',{@run_Callback});
function run_Callback(~,~)
tic;
progressDlg = uiprogressdlg(figT, 'Title','running', 'Indeterminate','on', 'Cancelable', 'on');
pool = gcp("nocreate");
if isempty(pool)
parpool('local');
pool = gcp("nocreate");
end
future = parallel.FevalFuture();
for i=1:5
future(i) = parfeval(pool, @run,1, 5e7);
end
addlistener(progressDlg, 'CancelRequested', 'PostSet', @(src,event)disp('run canceled'));
for i=1:5
% if progressDlg.CancelRequested
% cancelRun(future);
% end
[~,tot] = fetchNext(future);
disp(tot);
end
delete(progressDlg);
toc;
end
function tot = run(nb)
tot=0;
for i=1:nb
tot = tot + rand(1);
end
tot = tot/nb;
end
function cancelRun(future)
disp('run canceled');
cancel(future);
end
end
Accepted Answer
More Answers (0)
Categories
Find more on Startup and Shutdown in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!