- Declare two flag properties, ‘callback_running’ and ‘exit_callback.’ ‘callback_running’ indicates whether the long callback is running, and ‘exit_callback’ indicates whether to exit the long callback.
- Set the ‘callback_running’ to ‘true’ when entering the ‘longloop’ function and set it to ‘false’ when exiting.
- 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.
- Now, in the ‘UIFigureCloseRequest’ check if ‘callback_running’ is true, if it true, then set ‘exit_callback’ to true. The longloop function will terminate eventually.
How to determine whether certain callback functions of the App(GUI) are running when exit, and end them without error.退出UI程序时判断还有哪些回调函数正在执行,并结束它们?
4 views (last 30 days)
Show older comments
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方法。
谢谢!
0 Comments
Answers (1)
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:
Hope this helps!
0 Comments
See Also
Categories
Find more on 迁移使用 GUIDE 创建的 App 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!