来自 C# 进程类的无效操作异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1151903/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 09:27:43  来源:igfitidea点击:

Invalid Operation Exception from C# Process Class

c#.netvisual-studio-2008processinvalidoperationexception

提问by George2

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?

当我使用 VSTS 调试器查看类实例的属性时Process,很多属性都标有InvalidOperationException. 为什么?我做错了什么吗?

I am using VSTS 2008 + C# + .Net 2.0 to develop a console application.

我正在使用 VSTS 2008 + C# + .Net 2.0 来开发控制台应用程序。

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

在此处输入图片说明

采纳答案by Jon Skeet

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.

拍摄调试器图片时,您是否真的开始了该过程?这是我希望在Start()调用方法之前看到的屏幕截图。

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 Processobject until it's been started.

请注意,常见模式是创建一个ProcessStartInfo,填充它,然后调用静态Process.Start(startInfo)方法。这使得它在概念上更简单:在Process对象启动之前您看不到它。

回答by Chansik Im

Yes, this is expected behavior and it is clearly documented in MSDN as well.

是的,这是预期的行为,MSDN 中也清楚地记录了这一点。

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

例如,Process.BasePriority 属性可以在进程退出或进程尚未启动时抛出 InvalidOperationException 异常(请参阅MSDN 中的更多详细信息)。

回答by Pradeep Kumar

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.

许多属性都标有 InvalidOperationException,因为直到您启动该过程。对象“myProcess”与任何正在运行的进程都没有关联,因此它无法获取信息。

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

现在,当您在 if 语句中时,VSTS 调试器将能够显示与对象 myProcess 关联的大多数属性。这是因为,myProcess 对象现在与正在运行的进程“IExplore.exe”相关联。