Invalid Operation Exception from C# Process Class
Solution 1
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.
Solution 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".
Solution 3
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).
George2
Updated on July 19, 2022Comments
-
George2 almost 2 years
When I use VSTS debugger to see the properties of instance of class
Process
, many of the properties are marked withInvalidOperationException
. 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:
-
Ed S. almost 15 yearsBecause 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.
-
Jon Skeet almost 15 yearsThey 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.
-
George2 almost 15 yearsHi 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?
-
George2 almost 15 yearsThanks Joe, your reply makes senses.
-
George2 almost 15 yearsThanks Chansik, your reply makes senses!