简体   繁体   English

WaitForExit()抛出异常c#

[英]WaitForExit() throws exception c#

Below is my code to start the process, have put link for demo only.I want this process to run in background without opening browser. 下面是我启动该过程的代码,仅为演示添加链接。我希望此过程在后台运行而无需打开浏览器。 Also 2nd line throws exception 第二行抛出异常

Object reference not set to an instance of an object. 你调用的对象是空的。

var process=Process.Start("http://www.google.com");
process.WaitForExit();

Because when you start a process indirectly you won't get Process object (then in your case process is always null and second line throws an exception). 因为当您间接启动进程时,您将无法获得Process对象(然后在您的情况下,进程始终为null ,第二行抛出异常)。

Let me explain what I mean with indirectly : if you don't specify an executable but you give a document (or a resource) then it'll be executed through a shell verb. 让我用间接解释一下我的意思:如果你没有指定一个可执行文件但是你给了一个文档(或一个资源),那么它将通过一个shell动词来执行 In this case an existing process may be (re)used. 在这种情况下,可以(重新)使用现有过程。 In this cases Process.Start() will return null . 在这种情况下, Process.Start()将返回null

Try this: 试试这个:

  • Create an empty Word document 'c:\\test.docx'. 创建一个空的Word文档'c:\\ test.docx'。
  • Close all Word instances. 关闭所有Word实例。
  • Execute Process.Start(@"c:\\test.docx"); // Returns a Process instance 执行Process.Start(@"c:\\test.docx"); // Returns a Process instance Process.Start(@"c:\\test.docx"); // Returns a Process instance
  • Execute Process.Start(@"c:\\test.docx"); // Returns null 执行Process.Start(@"c:\\test.docx"); // Returns null Process.Start(@"c:\\test.docx"); // Returns null

Can you simply solve this? 你能简单解决这个问题吗? AFAIK you can't because Process uses ShellExecuteEx with a SHELLEXECUTEINFO structure to start the process. 您不能使用AFAIK,因为Process使用带有SHELLEXECUTEINFO结构的ShellExecuteEx来启动进程。 Reading SHELLEXECUTEINFO documentation for hProcess field you'll see that: 阅读hProcess字段的SHELLEXECUTEINFO文档,您将看到:

A handle to the newly started application. 新启动的应用程序的句柄。 This member is set on return and is always NULL unless fMask is set to SEE_MASK_NOCLOSEPROCESS. 除非将fMask设置为SEE_MASK_NOCLOSEPROCESS,否则此成员在返回时设置并始终为NULL。 Even if fMask is set to SEE_MASK_NOCLOSEPROCESS, hProcess will be NULL if no process was launched. 即使fMask设置为SEE_MASK_NOCLOSEPROCESS, 如果没有启动进程hProcess也将为NULL。 For example, if a document to be launched is a URL and an instance of Internet Explorer is already running, it will display the document. 例如,如果要启动的文档是URL并且Internet Explorer的实例已在运行,则它将显示该文档。 No new process is launched, and hProcess will be NULL. 没有启动新进程,并且hProcess将为NULL。 Note ShellExecuteEx does not always return an hProcess, even if a process is launched as the result of the call. 注意即使作为调用结果启动了进程,ShellExecuteEx也不会始终返回hProcess。 For example, an hProcess does not return when you use SEE_MASK_INVOKEIDLIST to invoke IContextMenu. 例如,当您使用SEE_MASK_INVOKEIDLIST调用IContextMenu时,hProcess不会返回。

Note if you're running a new process just to open an URL and get a server side generated file then you should follow Damien's suggestion and use a WebClient.DownloadFile() . 请注意,如果您正在运行新进程只是为了打开URL并获取服务器端生成的文件,那么您应该遵循Damien的建议并使用WebClient.DownloadFile()

Process.Start() can return a null reference: Process.Start()可以返回null引用:

Return Value Type: System.Diagnostics.Process 返回值类型:System.Diagnostics.Process

A new Process component that is associated with the process resource, or null, if no process resource is started (for example, if an existing process is reused). 与流程资源关联的新Process组件,如果未启动任何流程资源,则为null (例如,如果重用现有流程)。

(Emphasis mine) (强调我的)

When that happens, you'll get a NullReferenceException when trying to call WaitForExit() 当发生这种情况时,在尝试调用WaitForExit()时会出现NullReferenceException

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

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