Why does Process.WaitForExit throw a no-process exception, even if the process exists?

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 to run an IFilter extract in a document, we use a separate process, because some IFilters are known to be flaky.

Now this code works fine on Windows 7 and Server 2008 R2 mailboxes, but on Windows Server 2003 WaitForExit immediately throws "A process exception associated with this process object." The process exists and completes its task without problems.

Has anyone seen this? Can anyone shed some light on why WaitForExit will prevent this error?

Additional Information

If I put this code in a console application and run it, it works fine in the Windws Server 2003 window too, so it seems to be a special problem running in the "Service on Windows Server 2003" window.

+8
c # windows-server-2003
source share
1 answer

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

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

See ShellExecuteEx SHELLEXECUTEINFO :

Note that ShellExecuteEx may or may not return hProcess, depending on whether a new process has been started. This is the behavior you see.

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

Note. . Since you seem to be running the executable, it is a little surprising that hProcess not returning ShellExecuteEx . However, if you want you to get a process handle, using UseShellExecute = false is the right thing.

+13
source share

All Articles