如何在不触发 child 进程 'exit' 事件的情况下终止 child 进程?
How can I kill a child process without the child process 'exit' event triggering?
我正在生成一个 child 进程:
child = spawn("childprocess", [], {
detached: true
}
我正在观看 'exit' 活动:
child.on('exit', (code, signal) => {
// Do stuff
}
当我的应用程序退出时,我正在使用 taskkill
终止 child(因为这是 Windows 上的 运行):
exec(`taskkill /PID ${child.pid} /F /T`, (error, stdout, stderr) => {
// Do stuff
}
问题是,当进程被终止时,退出事件会触发(可以理解),但我不希望它触发。有什么方法可以删除事件侦听器吗?或者在不触发事件的情况下终止进程?我试过 child.removeListener('exit')
但没有用(也许是因为它是一个匿名函数?)。
I've tried child.removeListener('exit')
but that didn't work (maybe because it's an anonymous function?).
函数是否有名字并不重要。您只需将相同的函数对象传递给 removeListener
(或 off
):
var handler = (code, signal) => {};
child.on('exit', handler);
child.off('exit', handler);
我正在生成一个 child 进程:
child = spawn("childprocess", [], {
detached: true
}
我正在观看 'exit' 活动:
child.on('exit', (code, signal) => {
// Do stuff
}
当我的应用程序退出时,我正在使用 taskkill
终止 child(因为这是 Windows 上的 运行):
exec(`taskkill /PID ${child.pid} /F /T`, (error, stdout, stderr) => {
// Do stuff
}
问题是,当进程被终止时,退出事件会触发(可以理解),但我不希望它触发。有什么方法可以删除事件侦听器吗?或者在不触发事件的情况下终止进程?我试过 child.removeListener('exit')
但没有用(也许是因为它是一个匿名函数?)。
I've tried
child.removeListener('exit')
but that didn't work (maybe because it's an anonymous function?).
函数是否有名字并不重要。您只需将相同的函数对象传递给 removeListener
(或 off
):
var handler = (code, signal) => {};
child.on('exit', handler);
child.off('exit', handler);