HasExited returns wrong values ...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • piotr

    HasExited returns wrong values ...

    Hi,

    I have a problem with HasExited process property.
    The code is:

    ...
    if ( some_process.Ha sExited == false )
    {
    string name = some_process.Pr ocessName;
    }
    ...

    The error is :
    System.InvalidO perationExcepti on: Process has exited, so the requested
    information is not available.
    at System.Diagnost ics.Process.Ens ureState(State state)
    at System.Diagnost ics.Process.get _ProcessName()

    But if process has exited how come the if statement has been executed at all
    ???

    Regards
    Piotr



  • Jani Järvinen [MVP]

    #2
    Re: HasExited returns wrong values ...

    Piotr,
    [color=blue]
    > I have a problem with HasExited process property.
    > The error is :
    > System.InvalidO perationExcepti on: Process has exited, so the requested
    > information is not available.[/color]

    Some of the properties of the Process calls are not available after the
    associated process has exited, and ProcessName is one of those. So you
    should cache that value to another variable before the process has exited.

    Another thing. How are you constructing the Process object, and how are you
    starting the associated process? I often find that people seem to miss the
    fact that most of the overloads of the Start() method are static and return
    a new process object. This can cause trouble, as you are then possibly
    manipulating the wrong object instance.

    The following piece of errorneous code for example causes the
    System.InvalidO perationExcepti on you mentioned:

    --------------------------
    Process p = new Process();
    Process.Start(@ "C:\App\SomeApp .exe"); // wrong; static method returns new
    Process object
    Console.WriteLi ne("HasExited = "+p.HasExit ed); // exception here
    Console.WriteLi ne("Name = "+p.ProcessName );
    --------------------------

    The correct way would be to assign the return value of the static Start()
    method to "p", instead of creating a new Process instance with a call to
    "new".

    Hope this helps.

    --
    Regards,

    Mr. Jani Järvinen
    C# MVP
    Helsinki, Finland
    janij@removethi s.dystopia.fi



    Comment

    • cody

      #3
      Re: HasExited returns wrong values ...

      Maybe the process didn't start at all :)
      Or the status has changed between the two lines..
      A good idea would be to cache the process information right after you
      started the process.


      "piotr" <ksswd@poczta.f m> schrieb im Newsbeitrag
      news:eX4pUvFiFH A.2424@TK2MSFTN GP09.phx.gbl...[color=blue]
      > Hi,
      >
      > I have a problem with HasExited process property.
      > The code is:
      >
      > ...
      > if ( some_process.Ha sExited == false )
      > {
      > string name = some_process.Pr ocessName;
      > }
      > ...
      >
      > The error is :
      > System.InvalidO perationExcepti on: Process has exited, so the requested
      > information is not available.
      > at System.Diagnost ics.Process.Ens ureState(State state)
      > at System.Diagnost ics.Process.get _ProcessName()
      >
      > But if process has exited how come the if statement has been executed at[/color]
      all[color=blue]
      > ???
      >
      > Regards
      > Piotr
      >
      >
      >[/color]


      Comment

      • piotr

        #4
        Re: HasExited returns wrong values ...

        Hi
        [color=blue]
        > Some of the properties of the Process calls are not available after the
        > associated process has exited, and ProcessName is one of those. So you
        > should cache that value to another variable before the process has exited.[/color]

        That's right, if process has exited the ProcessName is unavailable. But at
        the same time HasExited value has to be TRUE. And I check ProcessName only
        if HasExited is FALSE.
        [color=blue]
        >
        > Another thing. How are you constructing the Process object, and how are[/color]
        you[color=blue]
        > starting the associated process? I often find that people seem to miss the
        > fact that most of the overloads of the Start() method are static and[/color]
        return[color=blue]
        > a new process object. This can cause trouble, as you are then possibly
        > manipulating the wrong object instance.
        >
        > The following piece of errorneous code for example causes the
        > System.InvalidO perationExcepti on you mentioned:
        >
        > --------------------------
        > Process p = new Process();
        > Process.Start(@ "C:\App\SomeApp .exe"); // wrong; static method returns new
        > Process object
        > Console.WriteLi ne("HasExited = "+p.HasExit ed); // exception here
        > Console.WriteLi ne("Name = "+p.ProcessName );
        > --------------------------
        >
        > The correct way would be to assign the return value of the static Start()
        > method to "p", instead of creating a new Process instance with a call to
        > "new".
        >[/color]

        This is my code:

        Process fp = new Process();
        fp.StartInfo.Ar guments = some_arguments;
        fp.StartInfo.Fi leName = some_filename;
        fp.Start();
        if ( fp.HasExited == false )
        {
        string name = some_process.Pr ocessName;
        }

        How should I change it ?

        Regards
        Piotr


        Comment

        • Jani Järvinen [MVP]

          #5
          Re: HasExited returns wrong values ...

          Hello!
          [color=blue]
          > This is my code:
          >
          > Process fp = new Process();
          > fp.StartInfo.Ar guments = some_arguments;
          > fp.StartInfo.Fi leName = some_filename;
          > fp.Start();
          > if ( fp.HasExited == false )
          > {
          > string name = some_process.Pr ocessName;
          > }
          >
          > How should I change it ?[/color]

          One error in the above code is that inside the if statement, you are reading
          the ProcessName property of "some_proce ss" when instead you should read it
          from "fp".

          Try this code instead and you see that it works (I'm using .NET 1.1 by the
          way, but it shouldn't make any difference):

          --------------------------
          Process fp = new Process();
          fp.StartInfo.Fi leName = @"C:\Windows\sy stem32\calc.exe ";
          fp.Start();
          if ( fp.HasExited == false )
          {
          string name = fp.ProcessName;
          Console.WriteLi ne("Name = "+name);
          }
          --------------------------

          Note that querying the ProcessName property can take a second or two even on
          a fast machine. So if your process really exits very fast, HasExited might
          be false on the if test, but not anymore when you read the process name,
          just as Cody pointed out earlier.

          --
          Regards,

          Mr. Jani Järvinen
          C# MVP
          Helsinki, Finland
          janij@removethi s.dystopia.fi



          Comment

          • piotr

            #6
            Re: HasExited returns wrong values ...

            Hi
            [color=blue]
            > Some of the properties of the Process calls are not available after the
            > associated process has exited, and ProcessName is one of those. So you
            > should cache that value to another variable before the process has exited.[/color]

            That's right, if process has exited the ProcessName is unavailable. But at
            the same time HasExited value has to be TRUE. And I check ProcessName only
            if HasExited is FALSE.
            [color=blue]
            >
            > Another thing. How are you constructing the Process object, and how are[/color]
            you[color=blue]
            > starting the associated process? I often find that people seem to miss the
            > fact that most of the overloads of the Start() method are static and[/color]
            return[color=blue]
            > a new process object. This can cause trouble, as you are then possibly
            > manipulating the wrong object instance.
            >
            > The following piece of errorneous code for example causes the
            > System.InvalidO perationExcepti on you mentioned:
            >
            > --------------------------
            > Process p = new Process();
            > Process.Start(@ "C:\App\SomeApp .exe"); // wrong; static method returns new
            > Process object
            > Console.WriteLi ne("HasExited = "+p.HasExit ed); // exception here
            > Console.WriteLi ne("Name = "+p.ProcessName );
            > --------------------------
            >
            > The correct way would be to assign the return value of the static Start()
            > method to "p", instead of creating a new Process instance with a call to
            > "new".
            >[/color]

            This is my code:

            Process fp = new Process();
            fp.StartInfo.Ar guments = some_arguments;
            fp.StartInfo.Fi leName = some_filename;
            fp.Start();
            if ( fp.HasExited == false )
            {
            string name = some_process.Pr ocessName;
            }

            How should I change it ?

            Regards
            Piotr



            Comment

            Working...