在电子应用程序中处理NodeJS的child_process

时间:2018-02-24 08:46:52

标签: javascript node.js electron child-process

所以我正在开发一个电子应用,应该在按下按钮时启动外部应用程序。它可以工作,但如果我关闭该应用并再次按下该按钮,它将启动该过程的几个实例。这是代码:

ipcMain.on("run_crystal", () => {
  var cp = require("child_process");
  executablePath = crystal + "\\CrystalDM\\Server\\CrystalDMServer.exe";
  var Server_child = cp.spawn(executablePath);
  executablePath = crystal + "\\CrystalDM\\Game\\CrystalDM.exe";
  var parameters = ["test"];
  var options = {cwd:crystal+"\\CrystalDM\\Game"};
  console.log("a intrat in start game si urmeaza sa ruleze " + executablePath)
  var Game_child = cp.execFile(executablePath, parameters, options, (error, stdout, stderr) =>{
    console.log(stdout)
    Game_child.kill("SIGINT");
    Server_child.kill("SIGINT");
    delete Game_child;
    delete Server_child;
    delete cp;
  });
});

3 个答案:

答案 0 :(得分:0)

这段代码可能被多次调用,你忘了在完成任务后删除事件监听器。我不知道怎么解决它,但试试这个:

ipcMain.once("run_crystal", () => {
   [your code here]
});

或者:

ipcMain.on("run_crystal", () => {
   [your code here]
   ipcMain.removeAllListeners("run_crystal");
});

答案 1 :(得分:0)

目前,我使用了这个解决方案:

  ipcMain.on("run_crystal", () => {
      if(if_started == 0){
      if_started = 1;
      var cp = require("child_process");
      console.log("mesajul a ajuns");
      executablePath = crystal + "\\CrystalDM\\Server\\CrystalDMServer.exe";
      var Server_child = cp.spawn(executablePath);
      executablePath = crystal + "\\CrystalDM\\Game\\CrystalDM.exe";
      var parameters = ["test"];
      var options = {cwd:crystal+"\\CrystalDM\\Game"};
      var Game_child = cp.execFile(executablePath, parameters, options,
      (error, stdout, stderr) =>{
         Game_child.kill("SIGINT");
         Server_child.kill("SIGINT");
         delete Game_child;
         delete Server_child;
         delete cp;
         delete parameters;
         delete options;
         if_started = 0;
      });
    }
});

答案 2 :(得分:0)

如果您不想多次启动流程,则必须跟踪已经启动的流程。

 let runningProcesses = {};

 ipcMain.on("run_crystal", (event, arg) => {
   var cp = require("child_process");
   executablePath = crystal + "\\CrystalDM\\Server\\CrystalDMServer.exe";

   if (runningProcesses[executablePath]){ // boolean value for a single process or int if you want to allow multiple instances runningProcesses[executablePath] < maxProcessCounts
     event.sender.send('process-running', { process: executablePath })
   } else {

     runningProcesses[executablePath] = true; // boolean value for a single process or int if you want to allow multiple instances

     var Server_child = cp.spawn(executablePath);

     Server_child.on('close', (code, signal) => {
       runningProcesses[executablePath] = false; // or delete runningProcesses[executablePath]; 
     });

     event.sender.send('process-started', { process: executablePath })

     executablePath = crystal + "\\CrystalDM\\Game\\CrystalDM.exe";
     var parameters = ["test"];
     var options = {cwd:crystal+"\\CrystalDM\\Game"};
     console.log("a intrat in start game si urmeaza sa ruleze " + executablePath)
     var Game_child = cp.execFile(executablePath, parameters, options,(error, stdout, stderr) =>{
        console.log(stdout)
        Game_child.kill("SIGINT");
        Server_child.kill("SIGINT");
        delete Game_child;
        delete Server_child;
        delete cp;
    });
  }
});