I'm spawning a child process:
child = spawn("childprocess", [], {
detached: true
}
And I'm watching the 'exit' event with:
child.on('exit', (code, signal) => {
// Do stuff
}
When my app exits, I'm killing the child by using taskkill
(because this is running on Windows):
exec(`taskkill /PID ${child.pid} /F /T`, (error, stdout, stderr) => {
// Do stuff
}
The problem is, that when the process is killed the exit event fires (understandably), but I don't want it to. Is there some way to remove the event listener? Or kill the process without triggering events? I've tried child.removeListener('exit')
but that didn't work (maybe because it's an anonymous function?).
I've tried
child.removeListener('exit')
but that didn't work (maybe because it's an anonymous function?).
Whether the function has a name or not doesn't matter. You simply need to pass the same function object to removeListener
(or off
):
var handler = (code, signal) => {};
child.on('exit', handler);
child.off('exit', handler);
Or call child.removeAllListeners('exit');
.
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加