473,324 Members | 2,473 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

HasExited returns wrong values ...

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

Nov 17 '05 #1
5 7859
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
ja***@removethis.dystopia.fi
http://www.saunalahti.fi/janij/
Nov 17 '05 #2
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" <ks***@poczta.fm> schrieb im Newsbeitrag
news:eX**************@TK2MSFTNGP09.phx.gbl...
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

Nov 17 '05 #3
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
Nov 17 '05 #4
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
ja***@removethis.dystopia.fi
http://www.saunalahti.fi/janij/
Nov 17 '05 #5
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

Nov 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Rudi Ahlers | last post by:
I tried the following: http://intranet/signup.php?Domain=newdomain.com, and it doesn't seem to return any values In my script I have the following: <? print $_POST; $DomainName = $_POST;...
0
by: Dan McGuffin | last post by:
I am trying to develop some vb.net code that will will store 1 or more processes in an array of processes, then loop through the array determining when each one has exited. Upon each one exiting, I...
4
by: pgp.coppens | last post by:
All, Seeing the behaviour below on DB2 v8 on zOS create table test(intcol integer); insert into test values (1); insert into test values (2); insert into test values (3); insert into test...
7
by: Douglas Buchanan | last post by:
I cannot access certain column values of a list box using code. I have a list box 'lstPrv' populated by the query below. SELECT tblPrv.fkPrvID, lkpCat.CatNm, lkpSrv.SrvNm, lkpCat.pkCatID,...
2
by: Roger Twomey | last post by:
I have an application that checks for browser, version and active X enabled. It uses the result for a simple if then. The issue I have is that Request.Browser.MajorVersion is returning the...
18
by: ben.carbery | last post by:
Hi, I have just written a simple program to get me started in C that calculates the number of days since your birthdate. One thing that confuses me about the program (even though it works) is...
0
by: ASP Developer | last post by:
I have a web service that returns a class when a web method is called. This class has a enum property with four values. These four values have default numbers. For example, Apple = 5 Orange...
1
by: Alan T | last post by:
I want to make sure my process is completed: Process.Start(); bool finished = false; while (!finsihed) { Thread.Sleep(1000); if (Process.HasExited) { Thread.Sleep(1000);
3
by: Dale Reed | last post by:
We're having a lot of problems getting visual studio to calculate even the most basic arithmetic correctly. For example: Dim MyValue AS Double = 2.155 * 100 This should give me 215.5 right?...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.