Skip to content

Instantly share code, notes, and snippets.

@olegam
Created August 26, 2015 17:24
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save olegam/10b87e3e54a859506a8d to your computer and use it in GitHub Desktop.
Save olegam/10b87e3e54a859506a8d to your computer and use it in GitHub Desktop.
var preventMultipleInstances = function(window) {
var socket = (process.platform === 'win32') ? '\\\\.\\pipe\\myapp-sock' : path.join(os.tmpdir(), 'myapp.sock');
net.connect({path: socket}, function () {
var errorMessage = 'Another instance of ' + pjson.productName + ' is already running. Only one instance of the app can be open at a time.'
dialog.showMessageBox(window, {'type': 'error', message: errorMessage, buttons: ['OK']}, function() {
window.destroy()
})
}).on('error', function (err) {
if (process.platform !== 'win32') {
// try to unlink older socket if it exists, if it doesn't,
// ignore ENOENT errors
try {
fs.unlinkSync(socket);
} catch (e) {
if (e.code !== 'ENOENT') {
throw e;
}
}
}
net.createServer(function (connection) {}).listen(socket);;
});
}
@hhammoud01
Copy link

Hello, thank you for this function. I am using atom electron to build a desktop application and I want to prevent multiple instances to run simultaneously. How do I use this (I'm new to atom electron) and Where do I add this, in my main.js file or preload.js file ?

Thank you for your help

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment