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.
bool moreThanOneInstanceAllowed()
{
return false;
}
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.",
AlertWindow::QuestionIcon);
w.setAlwaysOnTop(true);
w.showMessageBox(AlertWindow::InfoIcon, getApplicationName(), "Application is already running!!!", "ok");
w.toFront(true);
// 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.
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.
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.