7

I'm seeing a System.Diagnostics.Process.HasExited method throw an InvalidOperationException, but the message text property is not terribly useful as to why it was thrown. Under what conditions does this exception get thrown?

janw
  • 8,758
  • 11
  • 40
  • 62
Robert Davis
  • 2,255
  • 2
  • 21
  • 21

5 Answers5

7

I'm seeing the same message. It can happen if you do this:

System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo.FileName = "trash filename here.exe";
try
{
    proc.Start();
}
catch { }//proc should fail.
try
{
    if (proc.HasExited)
    {
        //....
    }
}
catch (System.InvalidOperationException e)
{
    //cry and weep about it here.
}

If proc.Start() failed above, you should get to cry and weep section, too. So, if you catch after proc.Start() be sure to catch at proc.HasExited (and MANY other of the System.Diagnostics.Process Methods.

lmat - Reinstate Monica
  • 7,289
  • 6
  • 48
  • 62
5

As Obalix correctly states, an InvalidOperationException is thrown when no process is attached to the Process object. This happens when a process has exited and Close or Dispose has been called on the Process object. Close releases all resources related to the process from memory. Before calling Close, this data was kept in memory to provide you (the programmer) with the information you want to know about the exited process, such as it's ExitTime and ExitCode.

Daniel A.A. Pelsmaeker
  • 47,471
  • 20
  • 111
  • 157
  • 1
    +=: Until you close/dispose the object, the underlying system keeps some resources to allow you to track the old process' state. When you close/dispose it, they are released, and so the PID number (and/or handle adresses, etc) may be REUSED for another brand new process. If afterwards you tried to query them for process' state, you *could* get an (cached) information that, for example, "PID=2688 is KILLED with exitcode 0x34" while the 2688 number *could* be already running again, and you *could* screw something up. Worse: you *could* get (uncached) info on the NEW process, and surely screw up. – quetzalcoatl Jul 03 '12 at 09:06
3

The documentation states that an InvalidOperation exception is thrown in no process is associated with the object.

Have you already started the process using Process.Start() or was the process disposed before you are accessing the HasExited property?

This post also deals whith the same issue.

Community
  • 1
  • 1
AxelEckenberger
  • 16,628
  • 3
  • 48
  • 70
2

If the above two answers take in mind that process's instance members aren't thread safe, so that might be the next place to start looking.

Brandi
  • 1,549
  • 4
  • 24
  • 32
1

Don't call Terminate.Close(), call Terminate.CloseMainWindoe() instead.

You may then issue a timed wait, check for HasExited and call Kill() if required.

SuperBiasedMan
  • 9,814
  • 10
  • 45
  • 73
Paul
  • 11
  • 1
  • Welcome to Stack Overflow! Please consider editing your post to add more explanation about what your code does and why it will solve the problem. An answer that mostly just contains code (even if it's working) usually wont help the OP to understand their problem. – SuperBiasedMan Nov 17 '15 at 10:33