none
Process throws an exception when getting its exist code sometimes: Process was not started by this object, so requested information cannot be determined

    Question

  • Hello everyone,

    I have an application which has a function that is used to compress files. If the compressed files are very large (more than 15GB), the application throws an exception as below:

     System.InvalidOperationException: Process was not started by this object, so requested information cannot be determined.

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

       at System.Diagnostics.Process.get_ExitCode()

    The code is :

    If(!compressProcess.HasExited)

    {

        // use taskkill to kill the compressProcess

         compressProcess.WaitForExit();  

    }

    //The application throws an exception here when compress big files

    If(compressProcess.ExitCode != 0)

    {

    }

    Anyone has any idea about how this happens?  Thanks.

    Wednesday, February 08, 2012 9:14 AM

Answers

  • This should also happen when you run this process from the Dos box command line. 

    You simply start a shell or Dos or cmd command or whatever you call it.

    However, can therfore be a wrong path, a non existing file, whatever it can be.


    Success
    Cor

    Wednesday, February 08, 2012 9:59 AM
  • If you try to get the ExitCode before the process has exited, the attempt throws an exception. Examine the HasExited property first to verify whether the associated process has terminated.
    Friday, February 10, 2012 6:53 AM

All replies

  • This should also happen when you run this process from the Dos box command line. 

    You simply start a shell or Dos or cmd command or whatever you call it.

    However, can therfore be a wrong path, a non existing file, whatever it can be.


    Success
    Cor

    Wednesday, February 08, 2012 9:59 AM
  • Hello Cor,

    Thank you for your areply, but what do you mean by ' can therfore be a wrong path, a non existing file, whatever it can be. '? do you mean the application throws this exception because the cmd command tries to compress an un-existing file?

    I tried to write the code as below, and the expextion's gone.

    ---------------------------------------------

    int existCode = 0;

    If(!compressProcess.HasExited)

    {

        // use taskkill to kill the compressProcess

         compressProcess.WaitForExit();  

       //The following statement works

         existCode = compressProcess.ExitCode ;

    }

    If(existCode != 0)

    {

    }

    ------------------------------

    The problem seems solved, but why this happens? The code above has no difference from the original code to me.



    • Edited by Violet Han Thursday, February 09, 2012 5:46 AM
    Thursday, February 09, 2012 1:51 AM
  • If you try to get the ExitCode before the process has exited, the attempt throws an exception. Examine the HasExited property first to verify whether the associated process has terminated.
    Friday, February 10, 2012 6:53 AM