Insidious Process Class or InvalidOperationException

By | September 4, 2020

I mean here .Net Process class from System.Diagnostics name space. Suddenly one application which worked for long time begins generate exceptions with the following text message: “Process has exited, so the requested information is not available“. The application was created for some testing purposes, quickly and frankly to say was not implemented well. I added exception type and call stack trace outputs to the exception catch statement and found that the exception type was InvalidOperationException. It looked strange because previously the code was executed successfully on the machines with the same Windows operating system. The call stack trace showed:


at System.Diagnostics.Process.EnsureState(State state)
at System.Diagnostics.Process.get_ProcessName()

The fragment of code which generated exception was:


if(File.Exists("d:\\temp\\console32.exe"))
{

   Process prcss = new Process();
   prcss.StartInfo.FileName = "d:\\temp\\console32.exe";
   prcss.Start();
   Thread.Sleep(500);
   Console.WriteLine("{0}({1})", prcss.ProcessName, prcss.Id);
}

The problem was that on that problematic machine console32.exe was executed so very quickly and at the moment when get_ProcessName was called the
console32.exe process already exited. ProcessName property has no sense because there was no such process.
Conclusion working with Process class object always remember that not all properties may be always accessible and contain information, it depends on process status and its security and access rights.

Leave a Reply

Your email address will not be published. Required fields are marked *