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.
Process.WaitForExit Method (System.Diagnostics)Sets the period of time to wait for the associated process to exit, and blocks the current thread of execution until the time has elapsed or the process has exited.
WaitForExit() Instructs the Process component to wait indefinitely for the associated process to exit. public: void WaitForExit(); C# Copy.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With