我在进程退出时有一个非常直接的事件处理程序,它想记录进程名称和退出代码。 无论我如何结束进程(从任务管理器调用 Kill()、结束任务或关闭应用程序),此代码都可以在我的本地计算机(Windows 10 专业版,64 位)中完美运行。 但是,Integration env(Windows Server 2016 DC,64 位)中相同代码的部署版本失败并显示以下错误。
<块引用>System.InvalidOperationException: 进程已退出,因此请求 信息不可用。在 System.Diagnostics.Process.EnsureState(State state) at 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();
}