23

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

Drew Noakes
  • 300,895
  • 165
  • 679
  • 742
George2
  • 44,761
  • 110
  • 317
  • 455
  • 1
    Please upload the details of exception message. – Chansik Im Jul 20 '09 at 06:24
  • 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)? – George2 Jul 20 '09 at 06:27
  • Most of the properties become available after the IExplore process starts. – Arsen Mkrtchyan Jul 20 '09 at 06:06
  • 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. – Cary Feb 11 '19 at 10:29
  • please check if your file name has space in between! if yes simply use double quotes for the path. – Malik Khalil Jul 01 '20 at 12:36

3 Answers3

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.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 1
    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)? – George2 Jul 20 '09 at 06:25
  • 1
    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. – Ed S. Jul 20 '09 at 06:27
  • 4
    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. – Jon Skeet Jul 20 '09 at 06:39
  • 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? – George2 Jul 20 '09 at 07:46
  • Thanks Joe, your reply makes senses. – George2 Jul 20 '09 at 07:46
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".

Kurubaran
  • 8,696
  • 5
  • 43
  • 65
Pradeep Kumar
  • 1,281
  • 7
  • 9
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).

Chansik Im
  • 1,473
  • 8
  • 13