Amit Garg
Amit Garg

Reputation: 59

Create object of System.diagnostics.Process

I am creating a new Systems.Diagnostics object:

System.Diagnostics.Process androidProcess= new System.Diagnostics.Process();

But my code results in InvalidOperationException exceptions, and I don't understand why.

BasePriority = 'androidProcess.BasePriority' threw an exception of type 'System.InvalidOperationException'
ExitCode = 'androidProcess.ExitCode' threw an exception of type 'System.InvalidOperationException'
ExitTime = 'androidProcess.ExitTime' threw an exception of type 'System.InvalidOperationException'

Why is my code generating these exceptions?

Thanks in advance.

Upvotes: 0

Views: 2355

Answers (2)

Cody Gray
Cody Gray

Reputation: 244981

The problem is that you cannot access the values contained in those properties until the process has started. Before the process has started, it does not have a process ID or a handle associated with it.

The documentation for the properties confirms this, indicating that an InvalidOperationException is thrown under one of the following conditions:

  • The process has exited.
    -or-
  • The process has not started, so there is no process ID.

The solution is to start the process that you've created first, and then get those properties as necessary.

Upvotes: 2

PVitt
PVitt

Reputation: 11770

BasePriority, ExitTime and ExitCode are read only. You cannot set these properties. They are set by the CLR or the started process itself.

Have a look at the MSDN:

Upvotes: 0

Related Questions