How to determine whether certain callback functions of the App(GUI) are running when exit, and end them without error.退出UI​程序时判断还有哪些回​调函数正在执行,并结​束它们?

6 views (last 30 days)
Hi,
I'm using App Designer to develop a GUI program. There is a loop which takes a lot of time in some callback functions. If I click exit button when one of the 'long' loop running, the app.UIFigure object will be deleted while the 'long' loop continue running. Then the statements in callback which access the components of App will cause error "Invalid or Deleted Object".
Is there a method that can determin whether a callback function is running and end it before app.UIFigure object deleted. Below is the situation I want to solve. After I search in Google, I find use dbstack function can get all of the running functions(scripts) in MATLAB. It's best if I can get the running App(class) methods directly.
classdef myapp < matlab.app.AppBase
properties
something
end
methods
%%%%%% a callback function takes a lot of time
%%%%%% 花费较长时间的回调函数
function longloop(app, event)
while expression
statements
end
end
function UIFigureCloseRequest(app, event)
%%%%% How to determine whether other callback functions are running and end them.
%%%%% 如何判断是否还有其他回调函数在执行,并且结束它们
delete(app)
end
%%%% function automatically generated自动生成的代码
% Code that executes before app deletion
function delete(app)
% Delete UIFigure when app is deleted
delete(app.UIFigure)
end
end
end
English is not my native language. If there are mistakes, please excuse me. Below is my problems described in Chinese.
Thank you!
我最近在用App Designer开发GUI程序,其中有一些回调函数包含较长的循环。在这些回调函数运行的过程中,如果我直接关闭GUI程序,app.UIFigure对象会被直接删除,但是其他回调函数仍在运行,并且会访问app的组件,这样就会产生“对象无效或已删除。”的错误。
我想知道在删除UIFigure对象前怎么判断是否有其他回调函数在执行,并且结束它们。在Google搜过之后,我发现可以通过`dbstack`函数获得MATLAB当前所有正在运行的函数。不知道有没有直接获得App(或者某个类)正在运行的methods方法。
谢谢!

Answers (1)

Poorna
Poorna on 10 Sep 2023
Hi renren cao,
I understand that you want to terminate the long running callback if it is running when the user exits the application. You can achieve this by communicating between the long running callback and the UIFigureCloseRequest function using flags.
Follow these steps:
  1. Declare two flag properties, callback_running and ‘exit_callback.’callback_runningindicates whether the long callback is running, and ‘exit_callbackindicates whether to exit the long callback.
  2. Set the ‘callback_running’ to true when entering the longloopfunction and set it to ‘false’ when exiting.
  3. Inside the loop that is running in the callback function add a check whether ‘exit_callback’ is set to true or not. If it is true then break from the loop, else continue with the loop.
  4. Now, in the UIFigureCloseRequest check if ‘callback_running’ is true, if it true, then set ‘exit_callbackto true. The longloop function will terminate eventually.
You can to refer to this answer. It addresses a similar problem.
Hope this helps!

Categories

Find more on 交互式控件和回调 in Help Center and File Exchange

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!