HasExited returns wrong values ...

  • Thread starter Thread starter piotr
  • Start date Start date
P

piotr

Hi,

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

...
if ( some_process.HasExited == false )
{
string name = some_process.ProcessName;
}
...

The error is :
System.InvalidOperationException: Process has exited, so the requested
information is not available.
at System.Diagnostics.Process.EnsureState(State state)
at System.Diagnostics.Process.get_ProcessName()

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

Regards
Piotr
 
Piotr,
I have a problem with HasExited process property.
The error is :
System.InvalidOperationException: Process has exited, so the requested
information is not available.

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.InvalidOperationException you mentioned:

--------------------------
Process p = new Process();
Process.Start(@"C:\App\SomeApp.exe"); // wrong; static method returns new
Process object
Console.WriteLine("HasExited = "+p.HasExited); // exception here
Console.WriteLine("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
(e-mail address removed)
http://www.saunalahti.fi/janij/
 
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.
 
Hi
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.

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.
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.InvalidOperationException you mentioned:

--------------------------
Process p = new Process();
Process.Start(@"C:\App\SomeApp.exe"); // wrong; static method returns new
Process object
Console.WriteLine("HasExited = "+p.HasExited); // exception here
Console.WriteLine("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".

This is my code:

Process fp = new Process();
fp.StartInfo.Arguments = some_arguments;
fp.StartInfo.FileName = some_filename;
fp.Start();
if ( fp.HasExited == false )
{
string name = some_process.ProcessName;
}

How should I change it ?

Regards
Piotr
 
Hello!
This is my code:

Process fp = new Process();
fp.StartInfo.Arguments = some_arguments;
fp.StartInfo.FileName = some_filename;
fp.Start();
if ( fp.HasExited == false )
{
string name = some_process.ProcessName;
}

How should I change it ?

One error in the above code is that inside the if statement, you are reading
the ProcessName property of "some_process" 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.FileName = @"C:\Windows\system32\calc.exe";
fp.Start();
if ( fp.HasExited == false )
{
string name = fp.ProcessName;
Console.WriteLine("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
(e-mail address removed)
http://www.saunalahti.fi/janij/
 
Hi
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.

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.
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.InvalidOperationException you mentioned:

--------------------------
Process p = new Process();
Process.Start(@"C:\App\SomeApp.exe"); // wrong; static method returns new
Process object
Console.WriteLine("HasExited = "+p.HasExited); // exception here
Console.WriteLine("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".

This is my code:

Process fp = new Process();
fp.StartInfo.Arguments = some_arguments;
fp.StartInfo.FileName = some_filename;
fp.Start();
if ( fp.HasExited == false )
{
string name = some_process.ProcessName;
}

How should I change it ?

Regards
Piotr
 
Back
Top