Externally pushed FolderItem

I have an OpenDocument event handler setup to handle FolderItems that are dragged over the application’s dock icon (macOS) and Windows desktop icon.
If the application is open and I drag a file over its dock icon on macOS, the application works as expected and fire up the OpenDocument event handler every time. But on Windows every time I drag a file it creates new instance of the application instead of handling the event.
Just to be clear, in case I drag multiple files together it does process them correctly, only when I drag another batch or single file it creates new instance of the application and handle it there.
I see many applications on Windows that handle this well so it can’t be some OS limitation for certain.
I can’t figure out why it would do that unless it’s flawed by design or am I missing some hidden option here?
(please excuse me if I don’t reply quickly, it’s late at night here and I’m totally burned out so I might fall asleep)

Windows and OS X have different conventions when it comes to launching multiple program instances. Windows will launch a new instance each time the program is executed. Your program will need to detect when another instance is already running (e.g. by having the first instance hold a Mutex that subsequent instances detect) and have the second instance send the document info to the first instance using some type of interprocess communication (e.g. an IPC socket).

Sorry for the long delay, that is very interesting Andrew!
So in a way Windows is more “advanced” as it tells developers to create applications both ways while on macOS it is strictly one way no matter what?
Anyhow, the only Socket I used so far is HTTPSecureSocket and even that was for very limited action:)
Is there any documentations, project example, or anything to Jump-Start my skills in making it work on Windows as it works on macOS by default?

Very simplified said:
Any App on macOS can only launch 1 instance from 1 location. There are worarounds but they are not the standard.
On macOS an App would handle additional Documents in additional Windows created by the App.

Thanks Sascha, well, at least macOS make my life easier in that regard :wink:
So can you please help me understand how do I mimic that behavior to “force” Windows to do the same?

Maybe the Application.OpenDocument Event delivers an Array of FolderItems or fires multiple times? In both cases you could just open another instance of the Window in which you handle Documents within your App.
Hand over the FolderItem to the new Window f.e. to keep a reference to the File and to prevent opening multiple Windows for the same file.

Thanks Sascha, I’ll try to figure out the best action.

BTW: If it still wont work, take a look into http://documentation.xojo.com/api/language/mutex.html to prevent multiple instances of your App on Windows OS.

That is a great idea but unfortunately I can’t seem to set it up. The Mutex works great to catch the OpenDocument event and prevent the second instance from processing the document, but I can’t figure out how to use the socket Connect and Write because I can’t place it in the OpenDocument event.

Here’s an example project that implements as a custom subclass of the Application class called SingleInstanceApp. The SingleInstanceApp class behaves like a normal Application class except its OpenDocument event will be raised for both ordinary documents and also those sent over IPC from another instance.

You can copy the SingleInstanceApp class into your project and then change the App superclass from Application to SingleInstanceApp.


You may need to replace all references to AbsolutePath with NativePath to make it work in recent editions of Xojo.

Oh this is awesome @Andrew Lambert !
Thank you very much for that, Greatly appreciated:)