Why would Process.WaitForExit throw a "no process" exception even when a process does exist?
Asked Answered
C

1

14

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());
            }
        }
    }

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

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.

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

Additional Info

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.

Casandracasanova answered 2/2, 2012 at 10:18 Comment(5)
That's very hard to explain with this code. Tinker with the service settings. Make sure the "interact with desktop" option is turned off. Post anything you find in the Application event log.Lavin
The error occurs when you don't get a HANDLE to the process (hence cannot wait for it). Make sure you are specifying UseShellExecute=false, to prevent reusing a process. Also can you describe the bitness 32 vs 64 of the service vs. the executable? Finally try Process Monitor and see what it shows happening.Serna
@Hans: Nothing is appearing any of the event logs (at least nothing that my code is not deliberately putting there). I'm not sure where to find the "interact with desktop" option"?Casandracasanova
@ben: I'll give UseShellExecute=false a try. Bitness is a good point. The Windows 2003 server is a 32 bit server so both service and executable run as 32 bit. However the Windows 7 ans 2008 R2 machines are 64 bit and both service and executable run as 64 bit. So it could be this is the difference not the OS version.Casandracasanova
@Ben: Post your "UseShellExecute" suggestion as an answer because that does fix this problem.Casandracasanova
S
18

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.

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.

See documentation on ShellExecuteEx's SHELLEXECUTEINFO:

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 is a lower-level function and creates a process directly, and simply passes the equivalent arguments. It always returns a process handle.

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.

Serna answered 2/2, 2012 at 17:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.