Ask questionssingleInstance Electron Manifest setting should handle command line arguments

Currently there are two ways of enforcing an app to be single instance.

  1. Set "singleInstance": true in the electron manifest json
  2. Write code in .NET to request the single instance lock and handle the second instance accordingly.

The first option seems to be the most reliable and robust. It works the fastest since it is being done in the javascript before the ASP .NET Core application launches (at least that is my understanding). However, one major problem is that you cannot handle the command line arguments to this second instance.

The alternative to this, is to set this to false and write the code to handle it ourselves, but this has several problems. Firstly, I can't get the callback to work, see #519 . Secondly, the second instance will always launch the splash screen, which is undesired if I have an instance running (and not a problem using the singleInstance in the manifest).

I really think both problems should be fixed, but I would be happy with one of the following two solutions. Solution 1: Allow the javascript to take arguments and pass them to .NET. Then we can just check for the lock and handle the arguments. Solution 2: Provide a new setting to the electron.manifest.json that will cause main.js to first check if it has the lock, and if it does not have the lock, don't display the Splashscreen, but continue to call the ASP .NET Core stuff so that we can handle the arguments.

I believe the only work around (assuming I can get the callback working or that gets fixed), is to handle showing the splash screen myself. That concerns me because I believe the app will seem more responsive at launch when the javascript launches the splash screen. My app seems to take a long time to load, and I am concerned that the splash screen will take a long time to show up if I write that code myself in .NET


Answer questions GregorBiswanger

Hi @Delpire, this feature is implemented for the next Electron.NET 13.5.1 release.

If the second instance is opened with arguments, the main application that has landed receives a focus. If you subscribe to this event, you can also request your new arguments.

As example:

Task.Run(async () =>
    var mainWindow = await Electron.WindowManager.CreateWindowAsync();
    mainWindow.OnFocus += async () =>
        if(await Electron.App.CommandLine.HasSwitchAsync("message"))
            var message = await Electron.App.CommandLine.GetSwitchValueAsync("message");
            await Electron.Dialog.ShowMessageBoxAsync(message);
Gregor Biswanger GregorBiswanger Germany Microsoft MVP, Intel Black Belt & Intel Software Innovator - is freelancer as consultant, author, speaker & trainer.
Github User Rank List