当进程的 window 关闭时触发事件,而不是当进程退出时

Fire an event when a process' window is closed, not when the process has exited

我开始一个过程:

var process = Process.Start("somestuff.bat");

然而,当一些事情完成后,它不会终止进程,只有 window 被关闭,所以自然地:

process.Exited += ...

永远不会开火。

那么,有没有办法以某种方式获取 window 句柄,或者 process 的任何方便之处,并在 window 关闭时触发一个方法?

您需要在流程上设置一些内容以使其触发事件

var process = Process.Start("somestuff.bat");
process.EnableRaisingEvents = true;
process.Exited += .. // note here, this runs in the process thread not the UI

退出然后触发

Use this make closing event..

XML收盘="Window_Closing"

private void Window_Closing(object sender, System.ComponentModel.CancelEventArgse)
{
    KillProcess();
}

试试这可能会解决您的问题

foreach (var process in Process.GetProcessesByName(".exe"))
{
    process.Kill();
}