Process Exited 事件处理程序方法未被调用

Process Exited event handler method is not being called

下面是代码...未调用 Process.Exited 事件处理程序方法...我还通过断点和所有这些检查了这一点。

Process f;

private void button3_Click_1(object sender, EventArgs e)
{
    f = new Process();
    f.StartInfo.FileName = "tutorial.mp4";
    f.EnableRaisingEvents = true;
    f.Exited += new EventHandler(f_Exited);
    f.Start();
}

private void f_Exited(object sender, System.EventArgs e)
{
    //some stuff not important
}

我刚查过。当您以这种方式使用 Process 时,它 不会 Process 对象附加到实际进程。例如,如果您尝试调用 Process.WaitForExit(),您将得到一个异常。所以当然也不能引发 Exited 事件; Process 对象无法知道哪个进程开始了,更不用说它何时退出了。

如果你想这样做,你要么需要启动实际的媒体播放器,向它提供必要的命令行参数或 DDE 动词来播放特定文件,要么你需要在之后搜索现有进程你开始这个过程,寻找你认为是刚刚开始的过程。

请注意,只有前一个选项是可靠的。在后一种情况下,理论上您可能会找到媒体播放器的其他实例(取决于您使用的媒体播放器以及它是否可以作为多个实例运行)。

我认为这是不可能的,因为当您打开这样的文件时,根本无法保证某个进程已启动。

假设没有为文件类型“.mp4”设置任何标准程序。然后 Windows 会要求用户选择打开文件的程序;但是,如果用户取消它并且根本不选择程序,则不会启动任何进程。因此,我相信在这种情况下,Exited 事件根本不会被触发,因为您不能依赖它。

我能想到的就是直接使用适当的命令行参数启动播放器,如下所示:

Process f;
private void  button3_Click_1(object sender, EventArgs e)
{
    f = new Process();
    f.StartInfo.FileName = "wmplayer.exe"; // or something other
    f.StartInfo.Arguments = @"c:\tutorial.exe"; // as for the wmplayer, you have to specify the whole path.
    f.EnableRaisingEvents = true;
    f.Exited += new EventHandler(f_Exited);
    f.Start();

}
private void f_Exited(object sender, System.EventArgs e)
{
    //some stuff not important
}