Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invalid Operation Exception from C# Process Class

Tags:

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

like image 253
George2 Avatar asked Jul 20 '09 05:07

George2


People also ask

Should I throw InvalidOperationException?

The InvalidOperationException exception should not be thrown for errors caused by invalid arguments. For invalid argument errors, throw ArgumentException or one of its derived types, such as ArgumentNullException or ArgumentOutOfRangeException. The ldflda CIL instruction throws InvalidOperationException.

What is exception c3?

An exception is defined as an event that occurs during the execution of a program that is unexpected by the program code. The actions to be performed in case of occurrence of an exception is not known to the program. In such a case, we create an exception object and call the exception handler code.

What is an exception in net?

In . NET, an exception is an object that inherits from the System. Exception class. An exception is thrown from an area of code where a problem has occurred. The exception is passed up the stack until the application handles it or the program terminates.


2 Answers

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.

like image 156
Jon Skeet Avatar answered Sep 21 '22 22:09

Jon Skeet


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".

like image 40
Pradeep Kumar Avatar answered Sep 19 '22 22:09

Pradeep Kumar