Preventing Duplicate App From Being Launched

If the user launches your app twice, you end up with two running instances of it. This is often not desirable. For instance the Remote Debugging app on windows will open multiple copies of itself if you double click the icon a few times. There’s never a call to have more than one instance launched. And, yes, users will double click your app icon to get back to it even though it’s already open.

Currently I use a mutex to determine if another copy of me is already running and then use an IPC socket to instruct the other app to come to the front and open a file if required then exit this copy. But it’s not every elegant.

Is there a better way to have double clicking your icon simply open the existing app? If you open Outlook Express multiple times, it seamlessly just switches to the running app.

I would love to hear an elegant solution for this.

Thats the way to do it on Windows & Linux
On OS X it automatically handles this for you - not so for Windows & Linux

http://documentation.xojo.com/index.php/Mutex
http://www.monkeybreadsoftware.eu/listarchive-realbasic-nug/2003-06-19-17.shtml

[quote=126875:@Stephen Dodd]Currently I use a mutex to determine if another copy of me is already running and then use an IPC socket to instruct the other app to come to the front and open a file if required then exit this copy. But it’s not every elegant.

Is there a better way to have double clicking your icon simply open the existing app? If you open Outlook Express multiple times, it seamlessly just switches to the running app.[/quote]

Why do you find Outlook Express does it differently ?

If a user click on a file to open the second instance of your application. What is the general way people use to transmit the file reference between the second application (that will close) and the first (that will now show up) ?

IPCSocket http://documentation.xojo.com/index.php/IPCSocket

The question just before gave me an idea.

  • When your app launches, try to connect through IPCSocket
  • If connect, send path of the file to open if needed, and quit
  • If not connect, switch to listen

It seems possible to avoid the use of Mutex. I do not know if it is better, though… Just different I guess.

Edit : Just tried with the Xojo example, modified. It works fine. There is an issue, though : when the second app disconnects, it makes an error in the first app and the socket becomes unresponsive. So I added this in the Error event of it :

me.close me.listen

Now no matter how many times I launch a new instance of the app it connects fine and quits.

Thanks.
I should try this approach.

It’s certainly do-able with IPCSockets. I use a mutex so that I don’t have to bother getting and waiting for a connection.

It just seems that other apps, like Outlook, must do this differently. I say this because Outlook seems to switch instantly to the correct app whereas my app must fully load up the copy, communicate and quit which seems less responsive.

[quote=127176:@Stephen Dodd]It’s certainly do-able with IPCSockets. I use a mutex so that I don’t have to bother getting and waiting for a connection.

It just seems that other apps, like Outlook, must do this differently. I say this because Outlook seems to switch instantly to the correct app whereas my app must fully load up the copy, communicate and quit which seems less responsive.[/quote]

The IPCSocket is because Franck asked about a way to inform the first app of the document to open. Made me realize Mutex+IPCSocket is not necessary in that case.

Microsoft uses all kinds of undocumented tricks to do things. Going through Msdn I found another way than Mutex but have to confess Microsoft explanations are just about as obscure as some Apple things, here :
http://msdn.microsoft.com/en-us/library/ms771662.aspx . Maybe this works at the system level, but the lack of specifics or sample code makes it difficult to figure.

In some C## forums, they also talk about getting the name of running processes and aborting if the name correspond to the launched app. It is a valid idea, I think, but it requires, like Mutex and IPCSocket, loading the app.

Basically, .NET handle this for you if you want and in a similar way as described by Stephen (which is the same method I use).
I do agree with Stephen that this is not very convenient to handle but, stated how Windows works I believe it’s the only way.

Btw, the use of the IPCSocket is only needed to forward the running app a possible document to open. If this is not needed, the Mutex is enough.
Now, handling the Mutex is easy, but the IPCSocket requires more work and it has an intrinsic difficulty in debugging it.
Moreover, IPCSocket is not exempt from problems on Windows.

One could write a template for such kind of application and reuse it many times.
On the other hand, since Xojo is an high level framework, it would be nice if it would provides a such functionality.

Looks like Mutex, and IPC for passing documents, is the only real way to go. Thanks everyone!