首页 > 解决方案 > 只允许两个 Electron Main Process 实例,但不能更多?

问题描述

当用户第二次运行可执行文件时,我想允许 Electron 启动第二个main进程,但如果它是第三个实例,则退出该进程。

我有以下代码用于处理与第一个不同的应用程序的第二个实例,但我不确定如何检测它是否是 Electron 3 的第三个实例。


电子主流程:Index.js

const { app } = require('electron')

const gotTheLock = app.requestSingleInstanceLock();

if (gotTheLock) {  
  app.on('second-instance', (argv, workingDirectory) => {
    //  Do some things
  })
} else {
  //  If instance does not have lock -
  //  require this file in the first instance without lock
  require('./host');

  //  Otherwise exit the new instance
  //  HOW CAN THIS BE ACHIEVED????
}

标签: javascriptnode.jselectroninstance

解决方案


推荐阅读