【问题标题】:Electron js child process not getting killedElectron js子进程没有被杀死
【发布时间】:2020-05-04 09:14:42
【问题描述】:

我正在编写一个电子 js 脚本来运行一个 .exe 文件。想法是当我单击“开始”按钮时,.exe 应该作为子进程启动,当我单击“停止”时,子进程应该被杀死。

我正在使用 IPC 进行通信。

const getScriptPath = () =>{
  if(process.platform==='win32'){
    return path.join(__dirname, 'dist_folder','pydoc.exe')

  }
}

const createPyProc =() =>{
  let script = getScriptPath()
  pyProc = require('child_process').execFile(script)  
  allProcess.push(pyProc)  

  }

}

const exitPyProc=() => {

    allProcess.forEach(function(proc){
      proc.kill();
    });


}
ipc.on('start_script',function(event){
  createPyProc()

})

ipc.on('stop_script', function(event){
  exitPyProc()

})

当我单击按钮启动时,我可以在任务管理器中看到子进程在电子主进程下启动,并在按下终止按钮后被终止。

问题: 1. 即使我关闭了电子窗口,其中电子下的子进程已经被杀死,pydoc.exe 的任务管理器中仍然会留下一个剩余的独立进程。

我的子进程命令是否正确?

 pyProc = require('child_process').execFile(script)  

【问题讨论】:

    标签: javascript node.js electron


    【解决方案1】:
      const subprocess = spawn(getScriptPath(), args);
    
      subprocess.stdout.on('data', data => {
        console.log(`Daemon stdout: ${data}`);
        resolve(data.toString());
        // Here is where the output goes
      });
      subprocess.stderr.on('data', data => {
        console.log(`Daemon stderr: ${data}`);
        resolve(data.toString());
        // Here is where the error output goes
      });
      subprocess.on('close', code => {
        console.log(`Successfully closed. ${code}`);
        // Here you can get the exit code of the script
      });
    
      ipc.on('stop_script', function(event){
        subprocess.kill(); 
      })
    

    【讨论】:

      猜你喜欢
      • 2010-12-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多