Invalid Operation Exception from C# Process Class
Asked Answered
W

3

24

When I use VSTS debugger to see the properties of instance of class Process, many of the properties are marked with InvalidOperationException. Why? Am I doing anything wrong?

I am using VSTS 2008 + C# + .Net 2.0 to develop a console application.

Here is my code:

System.Diagnostics.Process myProcess = new System.Diagnostics.Process();
myProcess.StartInfo.FileName = "IExplore.exe";
myProcess.StartInfo.Arguments = @"www.google.com";
myProcess.StartInfo.Verb = "runas";
myProcess.Start();

And a screenshot of the debugger:

enter image description here

Whity answered 20/7, 2009 at 5:57 Comment(5)
Most of the properties become available after the IExplore process starts.Gerry
Please upload the details of exception message.Perlie
Thanks, Chansik, detailed exception message (Exception.Message) is "No process is associated with this object." I have tested after process started, the values are becoming valid. I am confused why before process start the values are displayed as InvalidOperationException? It is for what purpose (why not have a default value to display)?Whity
The InvalidOperation is a typical exception which some programmers can meet when they try to start a process. One of the reasons can be the given argument was wrong. A good way to validate the arguments, is to test it on a cmd-line window. If the result will be expected, then you are safe.Conall
please check if your file name has space in between! if yes simply use double quotes for the path.Marietta
F
37

Had you actually started the process when the debugger picture was taken? That's the screenshot I'd expect to see before the Start() method is called.

Note that the common pattern is to create a ProcessStartInfo, populate it, and then call the static Process.Start(startInfo) method. That makes it conceptually simpler: you don't see the Process object until it's been started.

Fi answered 20/7, 2009 at 6:5 Comment(5)
Thanks Jon, I have tested after process started, the values are becoming valid. I am confused why before process start the values are displayed as InvalidOperationException? It is for what purpose (why not have a default value to display)?Whity
Because the getter is throwing an exception. It makes sense if you think about it; those properties will not be valid until the process has started or exited.Enhanced
They should definitely not return default values. If they did, one might assume that those values had been correctly returned from an actual process. What you're doing is the equivalent of asking a null reference for its length as a string... it doesn't have one, it isn't a string! Similarly you don't have a process to ask for its handle count etc. The exception is telling you you're doing something wrong: namely fetching properties before starting the process. That can never be a useful thing to do, and the exception is a much better indicator of that than default values would be.Fi
Hi Ed, for properties like basepriority, should be of some default value and it is just an int, why debugger cannot display the default value and why it reports InvalidOperationException?Whity
Thanks Joe, your reply makes senses.Whity
C
2

Many of the properties are marked with InvalidOperationException because until you start the process . The object 'myProcess' is not associated with any running process and hence it cannot get the information.

Try adding these statements, after the code to start the process

if (myProcess != null)  
{
  myProcess.WaitForExit();
   //or any other statements for that matter
}

Now, when you are inside the if statement, the VSTS debugger will be able to show most of the properties associated with the object myProcess. This happens because, myProcess object is now associated with a running process "IExplore.exe".

Copp answered 20/7, 2009 at 6:38 Comment(0)
P
1

Yes, this is expected behavior and it is clearly documented in MSDN as well.

For example, Process.BasePriority Property can throw an InvalidOperationException exception when the process has exited or the process has not started (see more details in MSDN).

Perlie answered 20/7, 2009 at 6:32 Comment(1)
Thanks Chansik, your reply makes senses!Whity

© 2022 - 2024 — McMap. All rights reserved.