What means an InvalidOperationException on Process.ExitTime "Process was not started by this object, so requested information cannot be determined."
Asked Answered
B

2

6

We have a tool that manages several processes of a computer and from time to time in uncertain conditions we get an InvalidOperationExceptionwhen accessing the MSDN: Process.ExitTime property.

The exception message says

process was not started by this object, so requested information cannot be determined

Does that mean that I can only get the exit time of a process that I've started myself? That's not documented anywhere.

Blennioid answered 5/12, 2017 at 8:29 Comment(1)
That was a design choice, it was written to assume that programmers will forget to call the Process.Dispose() method. Everybody does, that gets ugly in a big hurry, OS process objects are big. So it very eagerly releases the Process.Handle and that prevents it from being valid when the process terminates. You'd have to duplicate the handle and pinvoke GetProcessTimes() as a workaround.Multiracial
E
11

In order to access the exit time information, the Process class needs to have an open Handle for the processa. Whilst e.g. WaitForExit will temporarily create a handle, unfortunately it closes that handle before returning.

We can force the Process class to permanently open a handle to the process by accessing its Handle or SafeHandle properties1. However, we should notice that there is still some raciness here since between constructing the Process object and accessing the SafeHandle property the process could exit.

Start an instance of Notepad, find its process ID and edit it into the first line of Main here:

using System;
using System.Diagnostics;

namespace PlayAreaCSCon
{
  internal class Program
  {
    public static void Main(string[] args)
    {
      var p = Process.GetProcessById(18148);
      //var sh = p.SafeHandle;
      p.WaitForExit();

      Console.WriteLine(p.ExitTime);
      Console.ReadLine();
    }
  }
}

Once this program is running, exit Notepad and observe that we get the exception you've mentioned. Clean up and start another copy of Notepad. This time, after editing the process ID in, also uncomment the line that mentions SafeHandle.

Once this program is running once again, exit Notepad. Observe that this time we get the exit time. So you can hopefully see how this exception can arise and I think we'd both agree that the message is woefully incomplete.


aAnd it cannot magic one into existence for a process that has already exited.

1There are probably lots of extra entry points that indirectly cause these properties to be accessed and so side-effect the Handle into existence - but these are the most obvious routes for forcing the handle open.

Enneastyle answered 5/12, 2017 at 15:10 Comment(1)
Thanks for the effort you put on the answer.Blennioid
H
0

According to the example from MSDN at first you need to start a process, then you need to wait it to exit and at this point you can get a myProcess.ExitCode value. Probably you just filled some fields in created Process object but didn't started it. Also if you want to get an exit time of foreign process you didn't allowed to control, you need to use some monitors or hooks to get an exit time. Even though, the information you gave doesn't enough to grasp your intention, so provide it if you don't mind.

Herthahertz answered 5/12, 2017 at 14:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.