Created
March 4, 2024 11:54
-
-
Save andersonpem/68e14819de5976f8e57522dffcf8dc3b to your computer and use it in GitHub Desktop.
Electron - prevent multiple instances of the program to spawn.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { app, BrowserWindow } = require('electron'); | |
// This should be placed at the top of your main process script | |
const gotTheLock = app.requestSingleInstanceLock(); | |
if (!gotTheLock) { | |
// If we don't get the lock, quit immediately as another instance is running | |
app.quit(); | |
} else { | |
app.on('second-instance', (event, commandLine, workingDirectory) => { | |
// Someone tried to run a second instance, we should focus our window. | |
if (myWindow) { | |
if (myWindow.isMinimized()) myWindow.restore(); | |
myWindow.focus(); | |
} | |
}); | |
// Create myWindow, load the rest of the app, etc... | |
app.on('ready', () => { | |
// Your code to create the browser window, etc. | |
let myWindow = new BrowserWindow({/* Your configurations here */}); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment