I have create a juce application and wanted to have only one instance of the application running in the system.
Figured out the ‘moreThanOneInstanceAllowed()’ function to achieve the same.
I have set it to return false. This is perfect!!!
I am displaying an alert message if user launches the application second time, if we have one instance already running.
I handled it in the ‘anotherInstanceStarted()’ function.
Here is the code for it.
void anotherInstanceStarted (const String& /*commandLine*/)
//AlertWindow::showMessageBox(AlertWindow::AlertIconType::InfoIcon, getApplicationName(), "Application is already running!!!", "ok");
AlertWindow w ("AlertWindow demo..",
"This AlertWindow has a couple of extra components added to show how to add drop-down lists and text entry boxes.",
w.showMessageBox(AlertWindow::InfoIcon, getApplicationName(), "Application is already running!!!", "ok");
// This will get called if the user launches another copy of the app, but
// there's nothing that the demo app needs to do here.
The problem is if we launch the application second time and we have another window such as Notepad or paint opened on top of my application, the alert window is not brought in front and it remains behind these opened windows.
Is there a better way to handle this situation?
Thanks in advance!!!
Yeah, that’s how the OS works. It’s deliberately done that way to stop apps annoyingly popping up when you’re in the middle of doing something else.
Appreciate the prompt response, Jules.
I got it, however observed a different behavior.
Here are the steps for the different behavior, assuming that the application has the code as mentioned in the previous post.
- Start application.
- open any other application such as notepad or paint. Keep it on top of the application.
- Launch another instance of the application. Observe that the alert message does not comes up. It highlights in the toolbar only.
- Now bring other application(notepad or paint) on top of the application.
- launch again another instance of the application. Observe that this time alert message is displayed over the other application(notepad).
Why do we have 2 different behaviors here?
It’s the OS that decides whether to allow your window to come to the front or not, but I’m not an expert on the exact heuristics that it uses to figure out when that’s allowed.
Is there a way we can bring our application in front whenever user tries to launch second instance of the application.
I saw this behavior with gtalk, wherein if you have gtalk displayed on the screen and it is behind some other window, and user tries to launch the gtalk again, gtalk comes in front of the screen.
I don’t know. Maybe if the newly-launched app tried to bring the old one to the front, that’d be allowed.
Is there a way we can write something in ‘anotherInstanceStarted()’ function to bring the existing instance in front.
I tried toFront(true) and theMainWindow.setAlwaysOnTop(true). toFront() had no effect whereas setAlwaysOnTop() brings the window on top, however the window remains on top of everything just like taskmanager.
I really don’t know, I’ve never experimented with that kind of thing.