Prevent more than one instance of electron


#1

Hello, so i noticed right now there is nothing to prevent a user from running electron.exe (or w/e you name it) 10 times, and having 10 of the applications open.

This becomes an issue, when only one instance of the app will support a system tray. So I need a way to prevent any more instances opening up.

I saw this code, however it did not seem to have any effect.

anyone have any solutions or ideas?


How do I prevent multiple instances of the same application built with electron?
#2

Also

this is the error you get when a second application loads up trying to make a system tray icon. Is there a way to catch that error? if so I can simply close the extra applications down at that point.


#3

Could you catch that error and just exit? It won’t prevent them from starting another instance, but it won’t be around for long hopefully.


#4

Hey Leedohm!

yeah, that’s exactly what I was asking, is there a way to catch that error?

Looking at the source, all it seems to do is

if (!result)
    LOG(WARNING) << "Unable to create status tray icon.";
}

and im not sure how to catch that when all it is doing is putting it in the log.

appIcon = new Tray(__dirname + '/images/icon.png');

is what im calling, so I know i would have to catch the error there, im just not sure how.


#5

Where are you finding this code?

if (!result)
    LOG(WARNING) << "Unable to create status tray icon.";
}

#6

edit: also to note. Im using electron 0.30.5 . We must use this version due to it being the latest version electron-edge supports


#7

I’d file it as a bug on Electron and ask for support for that scenario. The problem is that it may be a platform-specific behavior which makes it harder to handle.

The other thing you can do is have some sort of sentinel that allows you to detect that there is more than one copy of the same app running. This can be as simple as a file on disk or just checking the process list for the current user.


#8

You may want to look at how the Atom Editor itself handles this case.

Atom uses a unix domain socket to listen for the opening of a new instance. If an instance already exists, it closes the newly created instance.

https://github.com/atom/atom/blob/master/src/browser/atom-application.coffee#L51

I am not sure your exact use case, but the one caveat of this approach is that it is possible for two instances to exist momentarily.

EDIT: I see now that your use case may be in a need of a way to completely prevent opening (or at least catch the error you mentioned). Good luck!


#9

Thank you both for your replies :smile:

@ leedohm I may do that so there is a report in and they can look at it for future users. However due to me being stuck on 0.30.5, a bugfix will not be able to help

I was able to do a work around, that is not ideal, but works. I call a C# function to get the number of processes. If its greater than 1 (as opening electron is 1 process no matter what) it will close down before attempting to open a new window or make a second tray.

was a bit tricky to get setup right, but seems to be working well enough right now.


#10

Looks like I ran into an even bigger issue with tray.

so my app, works. If you pull the app to any computer from our repo, it works.

If you rename the folder, it works.

However, the SECOND you copy the folder, the app will crash and say it can not make the system tray icon.

sadly I can not see anyone else with a similar issue here.
when looking at the line where it errors in the electron source the comments say

// This can happen if the explorer process isn’t running when we try to
// create the icon for some reason (for example, at startup).

but there is no way thats the issue

EDIT - Solution found for anyone googling
I feel like this would have to be a bug, but ill bring this up as a ticket. You must go to resources/yourappfolder/package.json and rename the app to anything else, add a 2, add anything, and the tray icon will work. Appearntly if you have two on your computer with the same name, the tray icon wont load and will error

Found it https://github.com/atom/electron/issues/2468


#11
/* Single Instance Check */
var iShouldQuit = app.makeSingleInstance(function(commandLine, workingDirectory) {
    if (mainWindow) {
        if (mainWindow.isMinimized()) mainWindow.restore();
        mainWindow.show();
        mainWindow.focus();
    }
    return true;
});
if(iShouldQuit){app.quit();return;}

#12

@thinkdj i have done same,
const shouldQuit = app.makeSingleInstance((commandLine, workingDirectory) => {
// Someone tried to run a second instance, we should focus our window.
if (mainWindow) {
if (mainWindow.isMinimized()) mainWindow.restore()
mainWindow.show()
mainWindow.focus()
}
})

if (shouldQuit) {
app.quit()
}

But when one instance of app is open it do not open new intsance(which is correct),but is not restoring the current instance(which is Minimized)
Any idea?