# anotherInstanceStarted not getting called

**URL:** <https://forum.juce.com/t/anotherinstancestarted-not-getting-called/6161>\
**Category:** General JUCE discussion\
**Created:** [January 8, 2011, 2:50am UTC](https://forum.juce.com/t/anotherinstancestarted-not-getting-called/6161 "2011-01-08T02:50:31Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![TheVinn](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/thevinn/32/806_2.png) [@TheVinn](https://forum.juce.com/u/TheVinn)\
**Post date:** [January 8, 2011, 2:50am UTC](https://forum.juce.com/t/anotherinstancestarted-not-getting-called/6161/1 "2011-01-08T02:50:31Z")

</div>

If a Juce application goes into a modal state in its JUCEApplication::initialise() function, and another instance is started, anotherInstanceStarted() doesn’t get called. This happens regardless of the return value of moreThanOneInstanceAllowed().

I might guess the reason is that the message loop doesn’t deliver the ActionListener message while in the modal state but to be honest, it all looks complicated to me and I am unsure.

My goal is to display an AlertWindow indicating to the user that a second instance is not allowed, since I take over the audio hardware. During initialise() there could be a lengthy startup for which I want progress or something. It is also possible that initialise() will fail and I am in the middle of showing an AlertWindow indicating the reason.

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [January 8, 2011, 10:57am UTC](https://forum.juce.com/t/anotherinstancestarted-not-getting-called/6161/2 "2011-01-08T10:57:19Z")

</div>

I don’t know of any reason why the message wouldn’t be received just because it’s a modal loop… It sounds more likely to me that the OS thinks your app hasn’t finished initialising, and is maybe stopping messages from being sent to it, or something like that. Have you tried doing your initialisation in an async callback after the initialisation method has finished?

---

<div class="post-metadata">

**Author:** ![chkn](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/chkn/32/3862_2.png) [@chkn](https://forum.juce.com/u/chkn)\
**Post date:** [January 8, 2011, 11:36am UTC](https://forum.juce.com/t/anotherinstancestarted-not-getting-called/6161/3 "2011-01-08T11:36:43Z")

</div>

never a open windows in intialisation, use a async updater

[http://www.rawmaterialsoftware.com/viewtopic.php?f=4&t=6054&start=0](http://www.rawmaterialsoftware.com/viewtopic.php?f=4&t=6054&start=0)

---

<div class="post-metadata">

**Author:** ![TheVinn](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/thevinn/32/806_2.png) [@TheVinn](https://forum.juce.com/u/TheVinn)\
**Post date:** [January 9, 2011, 12:00am UTC](https://forum.juce.com/t/anotherinstancestarted-not-getting-called/6161/4 "2011-01-09T00:00:07Z")

</div>

[quote=“chkn”]never a open windows in intialisation, use a async updater

[http://www.rawmaterialsoftware.com/viewtopic.php?f=4&t=6054&start=0[/quote]](http://www.rawmaterialsoftware.com/viewtopic.php?f=4&t=6054&start=0%5B/quote%5D)

Yeah but splash screens and progress screens need to be displayed and updated during initialization so that is not really an opton.

---

<div class="post-metadata">

**Author:** ![TheVinn](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/thevinn/32/806_2.png) [@TheVinn](https://forum.juce.com/u/TheVinn)\
**Post date:** [January 9, 2011, 12:01am UTC](https://forum.juce.com/t/anotherinstancestarted-not-getting-called/6161/5 "2011-01-09T00:01:16Z")

</div>

> [@jules](#):
>
> I don’t know of any reason why the message wouldn’t be received just because it’s a modal loop… It sounds more likely to me that the OS thinks your app hasn’t finished initialising, and is maybe stopping messages from being sent to it, or something like that. Have you tried doing your initialisation in an async callback after the initialisation method has finished?

I have not tried that, and it appears I was confused about anotherInstanceStarted(). I thought it went to the newly launched app. I was not aware that it is instead sent to the app already running. I will rerun the scenario and try to see if there is still a problem.

---

<div class="post-metadata">

**Author:** ![robiwan](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/robiwan/32/2446_2.png) [@robiwan](https://forum.juce.com/u/robiwan)\
**Post date:** [March 11, 2011, 4:26pm UTC](https://forum.juce.com/t/anotherinstancestarted-not-getting-called/6161/6 "2011-03-11T16:26:43Z")

</div>

Any news on this as I’ve encountered the same problem? I rely on anotherInstanceStarted being called (as it does nicely on Windows) in order to shut down the running instance.

Edit: And it doesn’t work postponing initialization after initialise has finished…

---

<div class="post-metadata">

**Author:** ![robiwan](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/robiwan/32/2446_2.png) [@robiwan](https://forum.juce.com/u/robiwan)\
**Post date:** [March 11, 2011, 4:51pm UTC](https://forum.juce.com/t/anotherinstancestarted-not-getting-called/6161/7 "2011-03-11T16:51:08Z")

</div>

???

In juce\_mac\_MessageManager.mm :

```auto
void MessageManager::broadcastMessage(const String&)
{
}
```

Maybe that’s why it doesn’t work ? 😉

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [March 11, 2011, 5:52pm UTC](https://forum.juce.com/t/anotherinstancestarted-not-getting-called/6161/8 "2011-03-11T17:52:39Z")

</div>

The mac uses a different mechanism to tell the running process when you try to open a document that it can use, and that ends up getting delivered by anotherInstanceStarted()

---

<div class="post-metadata">

**Author:** ![robiwan](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/robiwan/32/2446_2.png) [@robiwan](https://forum.juce.com/u/robiwan)\
**Post date:** [March 12, 2011, 5:08am UTC](https://forum.juce.com/t/anotherinstancestarted-not-getting-called/6161/9 "2011-03-12T05:08:05Z")

</div>

Hmm, ok, then how do I do to get it to work ? If I do /pathToMyApp/MyApp.app/Contents/MacOS/MyApp --myarg all I get in the terminal is:

`JUCE v1.53.46
Another instance is running - quitting...
`  
and my debug printout within anotherInstanceStarted shines with its absence…

And all that is called where the above is printed is MessageManager::broadcastMessage(…), and I am using the tip… :?

Edit: Ok, I don’t have any document associated with my application, and besides this works fine on windows…

---

<div class="post-metadata">

**Author:** ![robiwan](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/robiwan/32/2446_2.png) [@robiwan](https://forum.juce.com/u/robiwan)\
**Post date:** [March 12, 2011, 7:17am UTC](https://forum.juce.com/t/anotherinstancestarted-not-getting-called/6161/10 "2011-03-12T07:17:26Z")

</div>

[quote=“robiwan”]Hmm, ok, then how do I do to get it to work ? If I do /pathToMyApp/MyApp.app/Contents/MacOS/MyApp --myarg all I get in the terminal is:

`JUCE v1.53.46
Another instance is running - quitting...
`  
and my debug printout within anotherInstanceStarted shines with its absence…[/quote]

Let me clarify, I run one instance in the debugger (in which anotherInstanceStarted does NOT get called), and start the other from a terminal…

---

<div class="post-metadata">

**Author:** ![chkn](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/chkn/32/3862_2.png) [@chkn](https://forum.juce.com/u/chkn)\
**Post date:** [March 12, 2011, 7:42am UTC](https://forum.juce.com/t/anotherinstancestarted-not-getting-called/6161/11 "2011-03-12T07:42:56Z")

</div>

does this help?

[http://www.rawmaterialsoftware.com/viewtopic.php?f=4&t=6054&start=0#p33025](http://www.rawmaterialsoftware.com/viewtopic.php?f=4&t=6054&start=0#p33025)

---

<div class="post-metadata">

**Author:** ![robiwan](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/robiwan/32/2446_2.png) [@robiwan](https://forum.juce.com/u/robiwan)\
**Post date:** [March 12, 2011, 8:11am UTC](https://forum.juce.com/t/anotherinstancestarted-not-getting-called/6161/12 "2011-03-12T08:11:56Z")

</div>

No, it does not! Check the implementation of JUCEApplication::initialiseApp(…) + MessageManager::broadcastMessage(…). The mechanism that works on Windows is simply not implemented on Mac !

Edit: Well if this is not going to be implemented on Mac, could I at least suggest the following:

1. Add an overridable function to JUCEApplication called handleAnotherApplicationStartup(const String& cmdLine)
2. Call that function in initialiseApp instead of MessageManager::broadcastMessage
3. Call MessageManager::broadcastMessage in the default implementation of the new function

That way I can at least workaround the defunct Mac implementation.

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [March 12, 2011, 2:16pm UTC](https://forum.juce.com/t/anotherinstancestarted-not-getting-called/6161/13 "2011-03-12T14:16:44Z")

</div>

Yes, you’re right - on the mac it’s not implemented except to handle a document being opened. I’ll look into sorting something out there.

---

<div class="post-metadata">

**Author:** ![robiwan](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/robiwan/32/2446_2.png) [@robiwan](https://forum.juce.com/u/robiwan)\
**Post date:** [March 12, 2011, 3:19pm UTC](https://forum.juce.com/t/anotherinstancestarted-not-getting-called/6161/14 "2011-03-12T15:19:12Z")

</div>

Thnx Jules, much appreciated 🙂

---

<div class="post-metadata">

**Author:** ![robiwan](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/robiwan/32/2446_2.png) [@robiwan](https://forum.juce.com/u/robiwan)\
**Post date:** [March 12, 2011, 3:34pm UTC](https://forum.juce.com/t/anotherinstancestarted-not-getting-called/6161/15 "2011-03-12T15:34:38Z")

</div>

> [@jules](#):
>
> Yes, you’re right - on the mac it’s not implemented except to handle a document being opened. I’ll look into sorting something out there.

When you’re at it, can you fix so it is possible to set the return value of the “second” application ? With my solution it could be done as:

```auto
void MyApplication::handleAnotherInstanceStarted(const String& commandLine)
{
    setApplicationReturnValue(42);
    JUCEApplication::handleAnotherInstanceStarted(commandLine);
}
```

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [March 12, 2011, 6:28pm UTC](https://forum.juce.com/t/anotherinstancestarted-not-getting-called/6161/16 "2011-03-12T18:28:09Z")

</div>

I don’t understand your request about the return value - that’s already done, isn’t it?

---

<div class="post-metadata">

**Author:** ![robiwan](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/robiwan/32/2446_2.png) [@robiwan](https://forum.juce.com/u/robiwan)\
**Post date:** [March 12, 2011, 8:31pm UTC](https://forum.juce.com/t/anotherinstancestarted-not-getting-called/6161/17 "2011-03-12T20:31:07Z")

</div>

I don’t think so, since the second application won’t be called with initialise, so there’s nowhere for the application to set setApplicationReturnValue. Am I missing something ?

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [March 12, 2011, 10:35pm UTC](https://forum.juce.com/t/anotherinstancestarted-not-getting-called/6161/18 "2011-03-12T22:35:05Z")

</div>

> [@](#):
>
> I don’t think so, since the second application won’t be called with initialise,

initialise will always be called.

I’ve just checked in a version which adds broadcast messages for the mac, so hopefully that’ll sort out your other problem.

---

<div class="post-metadata">

**Author:** ![robiwan](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/robiwan/32/2446_2.png) [@robiwan](https://forum.juce.com/u/robiwan)\
**Post date:** [March 13, 2011, 7:50am UTC](https://forum.juce.com/t/anotherinstancestarted-not-getting-called/6161/19 "2011-03-13T07:50:00Z")

</div>

> [@jules](#):
>
> initialise will always be called.

Will initialise be called even for the second application when moreThanOneInstanceAllowed returns false and appLock-\>enter(0) also returns false (which  
is the case I’m trying to get to work) ? The code in JUCEApplication::initialiseApp does not look that way ?

> [@](#):
>
> I’ve just checked in a version which adds broadcast messages for the mac, so hopefully that’ll sort out your other problem.

Thanks, I’ll get back with a report how it turns out.

---

<div class="post-metadata">

**Author:** ![robiwan](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/robiwan/32/2446_2.png) [@robiwan](https://forum.juce.com/u/robiwan)\
**Post date:** [March 13, 2011, 9:49am UTC](https://forum.juce.com/t/anotherinstancestarted-not-getting-called/6161/20 "2011-03-13T09:49:27Z")

</div>

Sorry, but it doesn’t seem to work. I trace the call to MessageManager::broadcastMessage, but the first instance never gets a call to anotherInstanceStarted. Jules, I really need this to work so while the bugs are ironed out, could you please endulge me and add a protected function in JUCEApplication called handleAnotherInstanceStarted(const String& cmdLine), in which the default implementation calls MessageManager::broadcastMessage ??

I’ve done this on my own machine and using my own IPC messaging system it works perfectly, however, since there are more people than me in the project I really don’t want to distribute a patch to everybody, let alone having a git repo of my own…

[Next page](https://forum.juce.com/t/anotherinstancestarted-not-getting-called/6161.md?page=2)
