Process.StartTime is documented as not being accessible after the process has exited, but it is
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
https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.process.starttime?view=net-9.0#system-diagnostics-process-starttime
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
Article author
@dotnet-bot
Tagging subscribers to this area: @dotnet/area-system-diagnostics-process