# App Restart Procedure and potential anotherInstanceStarted() bug?

**URL:** <https://forum.juce.com/t/app-restart-procedure-and-potential-anotherinstancestarted-bug/12234>\
**Category:** General JUCE discussion\
**Created:** [January 20, 2014, 5:13pm UTC](https://forum.juce.com/t/app-restart-procedure-and-potential-anotherinstancestarted-bug/12234 "2014-01-20T17:13:06Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Joe](https://avatars.discourse-cdn.com/v4/letter/j/b9e5f3/32.png) [@Joe](https://forum.juce.com/u/Joe)\
**Post date:** [January 20, 2014, 5:13pm UTC](https://forum.juce.com/t/app-restart-procedure-and-potential-anotherinstancestarted-bug/12234/1 "2014-01-20T17:13:06Z")

</div>

Hi,

I'm trying implement a restart procedure that would happen&nbsp;after an update of our application.&nbsp;

I was following the discussion here:&nbsp;http://www.juce.com/forum/topic/application-restart

> modify your code so that you allow a second instance IF there is a specific command line option, and the second instance tells the previous instance to exit.

seems&nbsp;like a neat solution.&nbsp;

However I'm having an issue where&nbsp;**anotherInstanceStarted()** isn't being called as expected.&nbsp;

\* I'm running JUCE v3.0.1

\* Created a new basic window app from the Introjucer

\* Set **moreThanOneInstanceAllowed()** to return true

\* Added a logging statement to&nbsp;**anotherInstanceStarted()** in Main.cpp:

```
void anotherInstanceStarted (const String& commandLine)

    {

        DBG("Another Instance Started: " << commandLine);

    }
```

\* Run the application from Xcode (Debug configuration)

\* From the project directory run another instance of the app in the command line:

```
$ ./Builds/MacOSX/build/Debug/Restart_Test_GUI_2.app/Contents/MacOS/Restart_Test_GUI_2
```

From the documentation I would expect **anotherInstanceStarted()** to be called in the first instance I opened. However I don't get any callbacks in either app. Changing&nbsp;**moreThanOneInstanceAllowed()** to return false quits the second instance on launch as expected but it still doesn't give me a callback.&nbsp;

----------------

From digging around in JuceApplicationBase it looks like if **moreThanOneInstanceAllowed()** is true then the &nbsp;**sendCommandLineToPreexistingInstance()** method&nbsp;is never executed, and therefore never instantiates&nbsp;the **MultipleInstanceHandler** and registers it as a&nbsp;ActionBroadcastListener for the callback.&nbsp;&nbsp;  
&nbsp;

```
if ((! moreThanOneInstanceAllowed()) && sendCommandLineToPreexistingInstance())

    {

        DBG ("Another instance is running - quitting...");

        return false;

    }
```

-----

Am I missing something fundamental to get this to work? Or is this a potential bug in JUCE?

Is there an easier solution for an update procedure? My general aim was as follows:

\* Replace the old binary with the new one

\* Run a script to launch a new instance of the application (with some command line params)

\* Upon launch the initial instance detects it's an update and shuts itself down

&nbsp;

Thanks in advance,

Joe

---

<div class="post-metadata">

**Author:** ![Joe](https://avatars.discourse-cdn.com/v4/letter/j/b9e5f3/32.png) [@Joe](https://forum.juce.com/u/Joe)\
**Post date:** [January 28, 2014, 4:25pm UTC](https://forum.juce.com/t/app-restart-procedure-and-potential-anotherinstancestarted-bug/12234/2 "2014-01-28T16:25:00Z")

</div>

Following up on this, I still haven't managed achieve the desired functionality but I found some things that threw me off initially that might be worth looking into.&nbsp;

&nbsp;

**File::getSpecialLocation (File::currentExecutableFile)&nbsp;**returns a relational instead of an absolute path (on OSX anyway).&nbsp;

This means that:

&nbsp;

```
static NSString* getBroacastEventName()
{
    return juceStringToNS ("juce_" + String::toHexString (File::getSpecialLocation
            (File::currentExecutableFile).hashCode64()));
}
```

Will return a different broadcast ID if you run the app from Xcode or run it from the command line with a relational path:

i.e:

```
$ ./PATH/TO/APP
```

And thus will not receive the callback for another&nbsp;instance started.

---

**sendCommandLineToPreexistingInstance()** (juce\_ApplicationBase.cpp Line 248)&nbsp;will never get called if the app allows multiple instances. As far as I can see this should definitely be called if you allow multiple instances, but maybe I'm missing something?

If I change the code to the following I get the callback correctly when running the app from the command line (although annoyingly not yet in Xcode).&nbsp;

```
#if JUCE_HANDLE_MULTIPLE_INSTANCES

bool sendCommandLineResult = sendCommandLineToPreexistingInstance();

if ((! moreThanOneInstanceAllowed()) && sendCommandLineResult)

{

    DBG ("Another instance is running - quitting...");

    return false;

}

#endif
```

&nbsp;

Any ideas more than welcome! Is anyone else restarting their app a different way?

Thanks,  
Joe

---

<div class="post-metadata">

**Author:** ![Joe](https://avatars.discourse-cdn.com/v4/letter/j/b9e5f3/32.png) [@Joe](https://forum.juce.com/u/Joe)\
**Post date:** [January 28, 2014, 4:41pm UTC](https://forum.juce.com/t/app-restart-procedure-and-potential-anotherinstancestarted-bug/12234/3 "2014-01-28T16:41:19Z")

</div>

> If I change the code to the following I get the callback correctly when running the app from the command line (although annoyingly not yet in Xcode).&nbsp;

Actually that's not true, I do get the&nbsp;**anotherInstanceStarted()**&nbsp;once the focus has&nbsp;shifted back to the Xcode-ran app.&nbsp;

---

<div class="post-metadata">

**Author:** ![Joe](https://avatars.discourse-cdn.com/v4/letter/j/b9e5f3/32.png) [@Joe](https://forum.juce.com/u/Joe)\
**Post date:** [January 30, 2014, 12:56pm UTC](https://forum.juce.com/t/app-restart-procedure-and-potential-anotherinstancestarted-bug/12234/4 "2014-01-30T12:56:08Z")

</div>

OK so I got this working, albeitly in a different manner.&nbsp;&nbsp;I scratched all my previous code as it required some changes to JuceApplicationBase that I felt uncomfortable with and there were still inconsistencies when running it from Xcode and from the command line.&nbsp;

In the end I added a shell script to the project:

```
#!/bin/bash -e
sleep 1
open $1
exit 0
```

And added the following code to relaunch:

```
void SomeClass::restart() 
{
    String applicationPath = File::getSpecialLocation(File::currentApplicationFile).getFullPathName();
    String scriptPath = applicationPath + "/Contents/Resources/relaunch.sh";
    String relaunchCommand = scriptPath + " " + applicationPath;
    ScopedPointer<ChildProcess> scriptProcess = new ChildProcess();
    scriptProcess->start(relaunchCommand, (!ChildProcess::wantStdErr | !ChildProcess::wantStdOut)); JUCEApplication::getInstance()-\>quit();}
```

Much less headache and I don't know I didn't try this in the first place! Admittedly it's OSX only at the moment.

Cheers,

Joe

&nbsp;

---

<div class="post-metadata">

**Author:** ![oh3netid](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/oh3netid/32/63_2.png) [@oh3netid](https://forum.juce.com/u/oh3netid)\
**Post date:** [January 5, 2015, 6:13am UTC](https://forum.juce.com/t/app-restart-procedure-and-potential-anotherinstancestarted-bug/12234/5 "2015-01-05T06:13:37Z")

</div>

Hello, I'm also not seeing&nbsp;anotherInstanceStarted being called. I tried just adding a breakpoint to the callback on a fresh GUI project application and nothing happens when I run multiple instances of the app. Seems like there are a couple threads about this already so I apologize if I'm beating a dead horse but, it's not working on the default introjucer project.&nbsp;Any ideas?

&nbsp;
