当我尝试在ElectronJS应用程序中请求单示例锁时,收到错误ReferenceError: mainWindow is not defined。文档没有说明关于这个变量的任何要求。但是文档确实显示了变量myWindow的一个示例。这是怎么回事,我该怎么解决?
ReferenceError: mainWindow is not defined
myWindow
6vl6ewon1#
以下是我最终编写的代码,在Windows和MacOS上都是安全的:
var fs = require('fs') module.exports = class SingleInstance { constructor(){ this.appName = app.name.split(' ').join('-').toLowerCase() this.tmpPath = app.getPath('temp') + '\\' + this.appName + '\\' if (process.platform === 'darwin') this.tmpPath = this.tmpPath.split('\\').join('//') if (!fs.existsSync(this.tmpPath)) fs.mkdirSync(this.tmpPath) this.lockFilePath = this.tmpPath + 'lock.txt' if (this.isSecondInstance()){ process.exit() } else { this.updateLockFile() } } updateLockFile(){ fs.writeFileSync(this.lockFilePath, process.pid.toString()) } isSecondInstance(){ try { var pid = fs.readFileSync(this.lockFilePath).toString() try { var isAlreadyRunning = process.kill(pid, 0) //app is the second instance. terminate. return true } catch(err) { } } catch(err) { } } }
1条答案
按热度按时间6vl6ewon1#
以下是我最终编写的代码,在Windows和MacOS上都是安全的: