Created
August 26, 2015 17:24
-
-
Save olegam/10b87e3e54a859506a8d to your computer and use it in GitHub Desktop.
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
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);; | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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