Skip to content

Instantly share code, notes, and snippets.

@andersonpem
Created March 4, 2024 11:54
Show Gist options
  • Save andersonpem/68e14819de5976f8e57522dffcf8dc3b to your computer and use it in GitHub Desktop.
Save andersonpem/68e14819de5976f8e57522dffcf8dc3b to your computer and use it in GitHub Desktop.
Electron - prevent multiple instances of the program to spawn.
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