簡體   English   中英

Electron js子進程沒有被殺死

[英]Electron js child process not getting killed

我正在編寫一個 electron 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()

})

當我單擊按鈕開始時,我可以在任務管理器中看到子進程在 electron 主進程下啟動,並在按下終止按鈕后被終止。

問題: 1. 即使我關閉 electron window 下 electron 下的子進程已經殺死,pydoc.exe 的任務管理器中仍然留下一個剩余的獨立進程。

我的子進程命令是否正確?

 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