1

我在进程退出上有一个非常直接的事件处理程序,它想要记录进程名称和退出代码。无论我如何结束进程(调用 Kill()、从任务管理器结束任务或关闭应用程序),此代码都能在我的本地计算机(Windows 10 专业版,64 位)中完美运行。但是,集成环境(Windows Server 2016 DC,64 位)中相同代码的部署版本失败并出现以下错误。

System.InvalidOperationException:进程已退出,因此请求的信息不可用。在 System.Diagnostics.Process.EnsureState(State state) 在 System.Diagnostics.Process.get_ProcessName()

对此进行故障排除的任何解决方案/指针都会有所帮助。

事件处理程序如下所示:

private static void MyProcess_Exited(object sender, EventArgs e)
{
    var process = sender as Process;
    var processExitLog = new StringBuilder()
                    .AppendLine($"{process.ProcessName} exited with code {process.ExitCode} at {process.ExitTime.ToString("s")}.")
                    .AppendLine($"Total uptime: {(process.ExitTime - process.StartTime).ToString("c")}")
                    .AppendLine($"Running threads: {process.Threads.Count}")
                    .AppendLine($"Peak physical memory utilization was: {process.PeakWorkingSet64}.")
                    .ToString();

    Console.WriteLine(processExitLog);
    Console.WriteLine();
}
4

0 回答 0