What are the reasons why Process.HasExited might raise an InvalidOperationException?

I see the System.Diagnositics.Process.HasExited method throwing an InvalidOperationException, but the message text property is not very useful as to why it was selected. Under what conditions is this exception thrown?

+4
source share
5 answers

If the above two answers take into account that the process instance members are not thread safe, this could be the next place to look.

+2
source

I see the same message. This 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 also need to cry and cry. So, if you catch proc.Start() , be sure to catch in proc.HasExited (and proc.HasExited other System.Diagnostics.Process methods.

+7
source

When Obalix correctly states, an InvalidOperationException is thrown if no process is attached to the Process object. This happens when the process is completed and Close or Dispose was called in the Process object. Close frees all resources associated with the process from memory. Before calling Close this data was stored in memory to provide you (the programmer) with the information you want to know about the completed process, such as ExitTime and ExitCode .

+4
source

The documentation states that an InvalidOperation exception was thrown without a process associated with the object.

Have you already started the process with Process.Start() or was there a process that was before you reached the HasExited property?

This post also addresses the same issue.

+3
source

Do not call Terminate.Close() ; instead, call Terminate.CloseMainWindoe() .

You can then give a timeout, check HasExited and call Kill() if necessary.

0
source

Source: https://habr.com/ru/post/1306407/


All Articles