Description
Type of issue
Code doesn't work
Description
From the documentation:
InvalidOperationException
The process has exited.-or-
The process has not been started.
However, the following code does not throw an exception:
Process process = new Process();
process.StartInfo = new ProcessStartInfo(filePath) { UseShellExecute = true };
process.EnableRaisingEvents = true;
process.Exited += (sender, e) =>
{
(sender as Process)?.Refresh();
Console.WriteLine($"Process hasExited: {(sender as Process)?.HasExited}");
Console.WriteLine(
$"Start time in exited event handler: {(sender as Process)?.StartTime}"
);
}
Output:
Process hasExited: True
Start time in exited event handler: 14.03.2025 9:10:03
I believe reliably accessing StartTime after the process has exited would be beneficial. It would allow accurate identification of the process by combining the start time with the PID. Currently, using only the PID is not a reliable identifier for the process once it has exited. While this is how it works now, it does not match the documentation’s description of throwing an InvalidOperationException in this scenario.
Page URL
Content source URL
https://github.com/dotnet/dotnet-api-docs/blob/main/xml/System.Diagnostics/Process.xml
Document Version Independent Id
5527d631-d1a1-fb6e-8d2e-04438666fb96
Platform Id
241594df-b4d0-abb5-ce6f-dc2631eec7c4