为什么会Process.WaitForExit抛出"没有处理"例外,甚至当一个进程确实存在? [英] Why would Process.WaitForExit throw a "no process" exception even when a process does exist?

查看:356
本文介绍了为什么会Process.WaitForExit抛出"没有处理"例外,甚至当一个进程确实存在?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含这个代码窗口服务:

I have a windows service containing this code:

    public static void ExtractTextInner(string source, string destination)
    {   
        ProcessStartInfo startInfo = new ProcessStartInfo();
        startInfo.FileName = EXTRACTOR_EXE_FILEPATH
        startInfo.Arguments = "\"" + source + "\" \"" + destination + "\"";
        startInfo.CreateNoWindow = true;
        startInfo.WindowStyle = ProcessWindowStyle.Hidden;

        Process process = new Process();
        process.StartInfo = startInfo;

        process.Start();
        process.WaitForExit();
        int exitCode = process.ExitCode;
        process.Close();
        if (exitCode != 0)
        {
            switch (exitCode)
            {
                case 1:
                throw new ApplicationException("IFilter Extraction Failed");
                default:
                throw new ApplicationException("Unknown Exit Code:" + exitCode.ToString());
            }
        }
    }



这段代码的目的是在文档上运行的IFilter提取物,我们用一个单独的过程,因为有些IFilter的是出了名的片状。

The purpose of this code is run an IFilter extract on a document, we use a seperate process because some IFilters are notoriously flaky.

现在这个代码运行完全正常的Windows 7和Server 2008 R2的箱子,但在Windows Server 2003上的 WaitForExit 立即引发了没有与此Process对象关联的进程异常。这个过程确实存在,并没有问题,完成了它的任务。

Now this code runs perfectly fine on Windows 7 and Server 2008 R2 boxes but on a Windows Server 2003 the WaitForExit immediately throws a "There is no process associated with this Process object" exception. The process does exist and completes its task without a problem.

没有人见过这个?任何人都可以摆脱任何光线为什么WaitForExit将t指定这个错误?

Anyone seen this? Can anyone shed any light on why WaitForExit would thow this error?

其他信息

如果我在一个控制台应用程序如下代码并运行它正常工作的WINDWS Server 2003的箱为好,因此它似乎是在Windows服务器上的服务运行此特定问题的2003框。

If I place this code in a Console App and run it works fine on the Windws Server 2003 box as well, hence it would appear to be a specific problem running this in a Service on a Windows Server 2003 box.

推荐答案

在启动过程中,与的System.Diagnostics.Process 类,系统能够既可以使用的CreateProcess 的ShellExecuteEx Win32函数。当使用的CreateProcess 仅可执行文件可以启动。当使用的ShellExecuteEx ,可以使用从壳的开始 - >运行命令启动的任何文件。

When starting processes, with the System.Diagnostics.Process class, the system can either use CreateProcess or ShellExecuteEx Win32 function. When using CreateProcess only executable files can be started. When using ShellExecuteEx, any file which can be started using the "Start->Run" command from the shell.

不过这些都是启动过程的完全不同的方式。的ShellExecuteEx包括外壳,并且可以,例如,重复使用Word或Excel的现有实例打开一个文档,通过使用在 HKCR\<存储的信息;程序id> \shell\\ \\<动词GT; 注册表项。这可以通过使用DDE搜索,然后激活现有的Excel实例涉及例如

However these are completely different ways of starting processes. ShellExecuteEx involves the shell, and can, for example, re-use an existing instance of Word or Excel to open a document, by using the information stored under the HKCR\<progid>\shell\<verb> registry key. This may involve for example using DDE to search for and then activate an existing Excel instance.

查看的ShellExecuteEx 文档的 SHELLEXECUTEINFO

  • http://msdn.microsoft.com/en-us/library/windows/desktop/bb759784(v=vs.85).aspx

注意的ShellExecuteEx 可或可能不会返回取决于新的进程是否已开始了一个hProcess。这是您所看到的行为。

Note that ShellExecuteEx may or may not return an hProcess depending on whether a new process was started. This is the behavior which you are seeing.

的CreateProcess 是一个低级别的功能,并直接创造的过程,而简单地将相当于参数。它总是返回一个进程句柄

CreateProcess is a lower-level function and creates a process directly, and simply passes the equivalent arguments. It always returns a process handle.

注意:因为你似乎是启动一个可执行文件,它是一个有点出人意料,没有 hProcess 的ShellExecuteEx 返回。不过,如果你想确保你得到一个进程句柄,使用 UseShellExecute = FALSE 是做正确的事情。

Note: Since you seem to be starting an executable file, it is a bit surprising that no hProcess is returned by ShellExecuteEx. Nevertheless, if you want to ensure you get a process handle, using UseShellExecute = false is the correct thing to do.

这篇关于为什么会Process.WaitForExit抛出&QUOT;没有处理&QUOT;例外,甚至当一个进程确实存在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆