How to properly stop background while loop while closing app designer UI

15 views (last 30 days)
Hi all,
I had designed an app designer UI with a background while loop. Currently, I faced difficultyies in break the background while loop when I tried to close the UI. I use a closedRequestFunc and delete(app) operator to close the UI. However, some error just popped up after close as below:
------------------------------------------------------------------------------------------------------------------------------------------------------------------
Invalid or deleted object.
Error in MEMS_comm_int_V4_3_backgroud_timer_running/Background (line 612)
app.outputEditField.Value = fetchOutputs(result);
Error in MEMS_comm_int_V4_3_backgroud_timer_running/SimStartStopButton_callback (line 973)
Background(app);
Error using matlab.ui.control.internal.controller.ComponentController/executeUserCallback (line 378)
Error while evaluating DestroyedObject PrivateValueChangedFcn.
Error while evaluating TimerFcn for timer 'timer-60'
Invalid or deleted object.
------------------------------------------------------------------------------------------------------------------------------------------------------------------
It seems that even the app is closed the while loop is still working and here is my understanding: In closeRequestFunc, I reseted a flag making while statement to be false but, it looked like that, after closeRequestFunc, the program would return to the stop point before closeRequestFunc and finish its works in the last while loop.
Does anyone face this problem before and know how to solve it?

Accepted Answer

Walter Roberson
Walter Roberson on 10 Feb 2021
The background loop only gives up control at some locations, such as when it does drawnow() or pause() or uiwait() or waitfor(). Hypothetically, those are the only points at which the deletion of the objects can be processed. So between any of those and the next time you need the graphics object, use isgraphics() to test whether the object still exists.
(I said hypothetically, but I would need to look more closely into exactly what happens if a timer() or device callback such as ByteReceivedFcn deletes an object, as those callbacks can execute at the beginning of any MATLAB line.)
  2 Comments
WENCHIN LIU
WENCHIN LIU on 10 Feb 2021
Edited: WENCHIN LIU on 10 Feb 2021
That makes sense but I somehow found difficulties to get the result I expected. I tried isgraphics(), isprop(), isvalid(), and exist() and none of them works. It might be my problem though.
WENCHIN LIU
WENCHIN LIU on 10 Feb 2021
To whom it may concern,
I finally use try and catch in while loop. Normally, all operations will work as expected in the try; however, when something goes wrong the error will be catch and the statements in catch will be executed. Here, I use the break operator in the catch. Currently, it works faithfully as ruled.

Sign in to comment.

More Answers (0)

Categories

Find more on Code Execution 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!