Allow only one instance of an app on Windows

On Windows, it is possible run several instances of an app simultaneously. That happens if the user double clicks on the app icon or a document when the app is already running.

To avoid that, most developers use a Mutex in combination with an IPC socket (that was the suggestion in other threads of this board). I wrote a class for that. The class:

  • Creates a mutex to check if a second instance is running
  • Sends a message via IPC to the first instance to bring it to the front
  • Quits the second instance

And if the user double clicks on a file:

  • Creates a mutex to check if a second instance is running
  • Sends the file path via IPC to the first instance to open the file
  • Quits the second instance

But unfortunately the class does not work. The mutex is always created and the second instance tries to connect the first instance. But the “Connected” event of the IPC socket is never called. And I don’t know why. Maybe this is only a small issue, and I don’t see the mistake.

Can you please check the code and tell my why my class is not working? That would help me a lot. :slight_smile: Tested with Xojo 2016r3 and Windows 10.

Test project with the class:


Did you turn listen on on the first instance ? Otherwise the second instance cannot connect.

I updated the example project and added some quotes and fixes. The mutex does work. But the Socket not. The important part of the code is this:

  AppMutex=New Mutex(AppIdentifer) //Create the mutex object
  SocketOneInstance=New claSocketOneInstance(AppIdentifer+"Socket") //Create the socket
  //Try to start mutex 
  //This is the first instace: Listen for documents to open
  If MainInstance Then
    MsgBox "Main Instance: Listen"
    SocketOneInstance.Listen //Call listen on the first instance
    //This is the second instance: Tell the first instance to come to front
    MsgBox "Second Instance: Tell the first instance to come to front"
    SocketOneInstance.Command="Activate" //Set up a command. The command property was added to the socket by myself.
    SocketOneInstance.Connect //Connect the socket.
  End If

And in the “Connected” event of the socket:

[code] MsgBox “Connected”

//Second instance: Send command for activating or opening document
If Command<>"" Then
Write Command //Sends the command
Flush //Immediately
Command="" //Clear command variable
Close //Close the socket

//Quit the second instance, when the command was sent

End If[/code]

After Socket.Listen, the socket throws immediately error “0”. The same thing happens immediately after Socket.Connect. And the Connected event will never be called. Why?


Have you looked at the IPCSocket example ?

I copied the methods in there, and never had any issues.

Thank you Michael!

I had a look on the example and then again on my code and I found it. It was just a stupid mistake by myself:

SocketOneInstance=New claSocketOneInstance(AppIdentifer+"Socket") //Create the socket

And in the constructor of the Socket:

Constructor(Path As String) Path=SpecialFolder.Temporary.Child(Path).NativePath

Of course the code in the constructor should be:

Constructor(Path As String) Me.Path=SpecialFolder.Temporary.Child(Path).NativePath

So the Path property of the socket was never set. And I tried to listen / connect with an empty Path. I will offer the the classes for download here. Maybe someone else want to use them for other Windows projects.