Single Instance
Single instance locking is a mechanism that prevents multiple instances of your app from running at the same time. It is useful for apps that are designed to open files from the command line or from the OS file explorer.
Usage
To enable single instance functionality in your app, provide a SingleInstanceOptions
struct when creating your application:
The SingleInstanceOptions
struct has the following fields:
UniqueID
: A unique identifier for your application. This should be a unique string, typically in reverse domain notation (e.g., “com.company.appname”).EncryptionKey
: Optional 32-byte array for encrypting data passed between instances using AES-256-GCM. If provided as a non-zero array, all communication between instances will be encrypted.OnSecondInstanceLaunch
: A callback function that is called when a second instance of your app is launched. The callback receives aSecondInstanceData
struct containing:Args
: The command line arguments passed to the second instanceWorkingDir
: The working directory of the second instanceAdditionalData
: Any additional data passed from the second instance (if provided)
AdditionalData
: Optional map of string key-value pairs that will be passed to the first instance when subsequent instances are launched
Secure Communication
To enable secure communication between instances, provide a 32-byte encryption key. This key must be the same for all instances of your application:
Window Management
When handling second instance launches, you’ll often want to bring your application window to the front. You can do this using the window’s Focus()
method. If your window is minimized, you may need to restore it first:
How it works
Single instance lock using a named mutex. The mutex name is generated from the unique id that you provide. Data is passed to the first instance via NSDistributedNotificationCenter
Single instance lock using a named mutex. The mutex name is generated from the unique id that you provide. Data is passed to the first instance via a shared window using SendMessage
Single instance lock using dbus. The dbus name is generated from the unique id that you provide. Data is passed to the first instance via dbus