How to break a running function in an App Designer app?

I have a button in app designer that calls a function, which can sometimes take 15+ min to return. I'd like to have a second button which breaks the function call of the first so that I can change other UI elements and start the call over. What's the best way to do this?

Réponses (1)

Matt J
Matt J le 15 Jan 2018
Modifié(e) : Matt J le 15 Jan 2018

0 votes

Ctrl+C should abort the function call without aborting the app.
If that's not graceful enough, you will need to modify whatever loop button #1 is running so that it periodically check the state of some app property controlled by button #2.

6 commentaires

Thanks for the reply Matt. Is there a way to push a button and have it send the command Ctrl + C? That would work. Unfortunately checking a loop won't solve my issue - that would simply prevent the function from being called again if "break" criteria were detected. I only call the function one time, but sometimes it can take 15+ min to return so I need a way to abandon that function call by pushing a button.
According to this, no
However, I don't understand the part where you wrote "that would simply prevent the function from being called again if "break" criteria were detected". You would just need to reset the break flag at some point before attempting to rerun.
Thanks for looking into this Matt. Here's the code behind my statement, where butt_run is the button that calls the function sim_year after turning a lamp a different color. If I added a loop into this code for another button to break, it wouldn't do me any good as it takes 15 min for sim_year to return and that's what I need to break. Is there someway to embed a variable in sim_year that can be changed from the app designer app to return the function early? I have tried a global variable, but that didn't work.
% Button pushed function: butt_run
function butt_runButtonPushed(app, event)
app.lamp_run.Color = [0.5, 1.0, 0.0];
x = sim_year(app.in_it,slr_cur,batt_cur,app.b_chrg);
app.UITable.Data(j,i) = x.total;
app.lamp_run.Color = [0.8, 0.8, 0.8];
end
Matt J
Matt J le 16 Jan 2018
Modifié(e) : Matt J le 16 Jan 2018
I don't know why a global variable wouldn't work, but you can also pass app as an optional argument to sim_year() and have the abort flag polled inside the sim_year() workspace just as you would from the callback.
Matt, when you say "pass app" as an argument, what do you mean?
Matt J
Matt J le 16 Jan 2018
Modifié(e) : Matt J le 16 Jan 2018
I mean, pass the variable called "app" to sim_year as an optional 5th argument,
x = sim_year(app.in_it, slr_cur, batt_cur, app.b_chrg, app);
That way, sim_year will have access to everything that app does.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Develop Apps Using App Designer dans Centre d'aide et File Exchange

Question posée :

le 14 Jan 2018

Modifié(e) :

le 16 Jan 2018

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by