在进程窗口关闭时触发事件,而不是在进程退出时触发事件

时间:2015-08-13 07:15:49

标签: c# .net windows process

我开始使用以下过程:

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

但是,当某些内容完成后,它不会终止进程,只会关闭窗口,所以很自然:

process.Exited += ...

永远不会开火。

那么,有没有办法以某种方式获取窗口句柄,或者process的任何方便,并在该窗口关闭时触发方法?

3 个答案:

答案 0 :(得分:1)

Use this make closing event..

XML Closing =" Window_Closing"

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

答案 1 :(得分:0)

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

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

退出然后开火

答案 2 :(得分:-1)

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

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