How do I create an app that only allows one instance of itself to run?

55 views (last 30 days)
If I run my app created in app designer, the GUI launches and If I re-run my app again, another GUI window opens. I want to make sure only single GUI window is open at a time. Meaning, if my GUI window is open, I do not want to open another window until the initial window is closed. Is this possible with the app designer?

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 11 Sep 2023
Edited: MathWorks Support Team on 26 Oct 2021
In GUIDE, you can check the option "GUI allows only one instance to run" from within the GUI Options dialog. This is described in more detail on this documentation page: 
In AppDesigner, if you are using R2020b or later releases, you can select whether your app can run multiple instances at a time or only a single instance.
To change the run behavior of your app, select the App node from the Component Browser. Then, from the Code Options section of the Inspector tab, select or clear Single Running Instance.
When Single Running Instance is selected and you run the app multiple times, MATLAB reuses the existing instance and brings it to the front rather than creating a new one. When Single Running Instance is not selected, then a new app instance is created each time you run it, while existing instances of the app also continue to run.
You can see the details in the following URL page: 
  2 Comments
Xingwang Yong
Xingwang Yong on 12 Jan 2024
How about the release prior to R2020b? Seems no way to achieve it. App desinger is so confused that some obvious things that exist in guide while not supported in App designer.
Nandha
Nandha on 10 Apr 2024 at 15:40
Unfortunately, App Designer does not have an in-product way to support this prior to R2020b. You can definitely implement this logic by yourself based on your requirements. For instance you can have a function (like the startup function) which launches the app and performs the singleton logic in there. Here's a simple example that demonstrates deleting the duplicate app instance if an existing app instance is already found:
% Code that executes after component creation
function startupFcn(app)
% Unique identifier for your app instance
appID = 'MyUniqueAppID';
% Attempt to retrieve an existing app instance
existingApp = getappdata(0, appID);
if isempty(existingApp)
% No existing instance, store this instance using a unique identifier
setappdata(0, appID, app);
else
% Existing instance found, bring it to the front
figure(existingApp.UIFigure);
% Optionally, close the current instance if it's a duplicate
delete(app);
return; % Exit the startup function to prevent further initialization
end
end

Sign in to comment.

More Answers (0)

Categories

Find more on Develop Apps Using App Designer in Help Center and File Exchange

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!