繁体   English   中英

创建System.diagnostics.Process的对象

[英]Create object of System.diagnostics.Process

我正在创建一个新的Systems.Diagnostics对象:

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

但我的代码导致InvalidOperationException异常,我不明白为什么。

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'

为什么我的代码会产生这些异常?

提前致谢。

问题是,在进程启动之前,您无法访问这些属性中包含的值。 在进程启动之前,它没有进程ID或与之关联的句柄。

属性的文档确认了这一点,表明在下列条件之一时抛出InvalidOperationException

  • 这个过程已经退出。
    -要么-
  • 该进程尚未启动,因此没有进程ID。

解决方案是启动您首先创建的进程,然后根据需要获取这些属性。

BasePriority,ExitTime和ExitCode是只读的。 您无法设置这些属性。 它们由CLR或启动过程本身设置。

看看MSDN:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM