Reputation: 109
I have just built my first Electron app. But I noticed that 3 services of my app running in background at any time. When the app is activated by the global keyboard shortcut, one instance moves to the active app section.
On exiting, only one instance is gone while the other two keeps running. And I cannot start the app again without stopping these services manually from Task Manager.
app.js
const { app, BrowserWindow, globalShortcut } = require("electron");
const path = require("path");
const url = require("url");
let win = null;
var shouldQuit = app.makeSingleInstance(function(commandLine, workingDirectory) {
// Someone tried to run a second instance, we should focus our window.
if (win) {
win.show();
}
});
if (shouldQuit) {
app.quit();
return;
}
app.on("ready", () => {
win = new BrowserWindow({
width: 1000,
height: 600,
transparent: true,
frame: false
});
win.on("close", () => {
win = null;
});
win.loadURL(
url.format({
pathname: path.join(__dirname, "app", "index.html"),
protocol: "file",
slashes: true
})
);
win.once('ready-to-show', () => {
win.show()
});
win.on("blur", () => {
win.hide();
});
win.on('window-all-closed', () => {
globalShortcut.unregisterAll();
if (process.platform !== 'darwin') {
app.quit()
}
});
const ret = globalShortcut.register('CommandOrControl+L', () => {
if(win == null) {
globalShortcut.unregisterAll();
return;
}
win.show();
});
if (!ret) {
console.log('registration failed')
}
app.on('will-quit', () => {
// Unregister all shortcuts.
globalShortcut.unregisterAll();
});
app.on('before-quit', () => {
win.removeAllListeners('close');
globalShortcut.unregisterAll();
win.close();
});
});
The previous error I encountered was that the GlobalShortcut was not deregistered when closing, so I added it to before-quit and window-all-closed events. and that solved it.
Edit: The above problem only happens on production code. During development, all 3 instance closed together. I am using Windows 10 and Electron 1.6.11.
Upvotes: 2
Views: 4015
Reputation: 739
Just add this to your main javascript file where you included electron module
app.on('window-all-closed', () => {
app.quit()
})
Upvotes: 3