- 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 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(1 个)
Poorna
2023-9-10
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 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!