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 开发者_Go百科'System.InvalidOperationException'
ExitTime = 'androidProcess.ExitTime' threw an exception of type 'System.InvalidOperationException'
Why is my code generating these exceptions?
Thanks in advance.
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.
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:
- BasePriority
- ExitCode
- ExitTime
精彩评论