开发者

Process Exited event not firing from within webservice

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 MessageEve开发者_JAVA百科ntArgs(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?


As it turns out the problem was caused by the executable that I was trying to invoke.

Unfortunately, this 3rd party executable was a port of a UNIX command that was being run through a kind of emulator. The executable was designed to output messages to the output and error streams as one would expect. However, the toolkit our vendor used to port the binaries over to Windows does not use the standard output streams.

When I stepped through the web service and manually invoked the process from the command line, I saw the emulator display an error dialog box. From the C# point of view, the process does not complete unless the [OK] button is clicked on the dialog box hence the "Exited" event never fires.

After speaking with our vendor for the executable I learned it is not fully supported in 64-bit Windows. I installed the web service on a 32-bit environment and everything was fine.


What's the process you are running in there? Since you mentioned its a console application, perhaps, it was waiting for more input? Since running this as a webservice, is the executable running under the same permissions as the ASP webservice? It could be that the web-service is not releasing the executable as that is loaded in process, or that the web-service is designated to run forever until IIS gets restarted and then the Process's Exit event may get fired.

It has come to my attention also, that there is no using clause around the Process's instantiated object i.e.

using (Process proc = new Process())
{
}

Edit: Also, please see here a link to a similar concept. The only thing after comparing the results is that the property WindowStyle is set...

ps.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜