C# Process Exited event not firing from within webservice

Posted by davidpizon on Stack Overflow See other posts from Stack Overflow or by davidpizon
Published on 2010-06-01T20:49:43Z Indexed on 2010/06/01 20:53 UTC
Read the original article Hit count: 647

Filed under:
|
|

I am attempting to wrap a 3rd party command line application within a web service.

If I run the following code from within a console application:

Process process= new System.Diagnostics.Process();
process.StartInfo.FileName = "some_executable.exe";

// Do not spawn a window for this process
process.StartInfo.CreateNoWindow = true;
process.StartInfo.ErrorDialog = false;

// Redirect input, output, and error streams
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.RedirectStandardInput = true;
process.EnableRaisingEvents = true;


process.ErrorDataReceived += (sendingProcess, eventArgs) => {
    // Make note of the error message
    if (!String.IsNullOrEmpty(eventArgs.Data))
        if (this.WarningMessageEvent != null)
            this.WarningMessageEvent(this, new MessageEventArgs(eventArgs.Data));
};

process.OutputDataReceived += (sendingProcess, eventArgs) => {
    // Make note of the message
    if (!String.IsNullOrEmpty(eventArgs.Data))
        if (this.DebugMessageEvent != null)
            this.DebugMessageEvent(this, new MessageEventArgs(eventArgs.Data));
};

process.Exited += (object sender, EventArgs e) => {
    // Make note of the exit event
    if (this.DebugMessageEvent != null)
        this.DebugMessageEvent(this, new MessageEventArgs("The command exited"));
};

process.Start();
process.StandardInput.Close();
process.BeginOutputReadLine();
process.BeginErrorReadLine();

process.WaitForExit();

int exitCode = process.ExitCode;
process.Close();
process.Dispose();

if (this.DebugMessageEvent != null)
    this.DebugMessageEvent(this, new MessageEventArgs("The command exited with code: " + exitCode));

All events, including the "process.Exited" event fires as expected. However, when this code is invoked from within a web service method, all events EXCEPT the "process.Exited" event fire.

The execution appears to hang at the line:

process.WaitForExit();

Would anyone be able to shed some light as to what I might be missing?

© Stack Overflow or respective owner

Related posts about c#

Related posts about web-services