Forked from olegam/electron-prevent-multiple-instances.js
Last active
September 4, 2015 21:24
-
-
Save mnichols/8c2ccc0e2122dd294488 to your computer and use it in GitHub Desktop.
This file contains hidden or 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