简体   繁体   English

Electron js子进程没有被杀死

[英]Electron js child process not getting killed

I'am writing a electron js script to run an.exe file.我正在编写一个 electron js 脚本来运行一个 .exe 文件。 Idea is when i click on button 'start'.exe should start as child process and when i click on 'stop' the child process should get killed.想法是当我单击“开始”按钮时。exe 应该作为子进程启动,当我单击“停止”时,子进程应该被杀死。

I'am using IPC for communication.我正在使用 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()

})

when i click on button start i can see in task Manager child process starts under electron main process and it is get killed after pressing kill button.当我单击按钮开始时,我可以在任务管理器中看到子进程在 electron 主进程下启动,并在按下终止按钮后被终止。

problem: 1. Still a residual independent process is left in task manager of pydoc.exe even i close electron window where child process under electron already killed.问题: 1. 即使我关闭 electron window 下 electron 下的子进程已经杀死,pydoc.exe 的任务管理器中仍然留下一个剩余的独立进程。

Is my child process command is correct?我的子进程命令是否正确?

 pyProc = require('child_process').execFile(script)  
  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(); 
  })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM