Run CMD / Java application using C#

Few days ago I needed to write a piece of code to call ANT file that builds some java files using the following CMD command ant –f build.xml. The process is rather straight forward task.  Use System.Diagnostics.Process that’s all, Easy.

To get the output result from the child process I redirect the standard output and the standard error and then read from the stream.

String output = process.StandardOutput.ReadToEnd();
String error = process.StandardError.ReadToEnd();

I developed my application under vista and after testing everything seems OK, until I moved it to the server (windows 2003). Whenever I run the process it hangs and never exist???

After struggling, the problem wasn’t OS related it was rather normal behavior if you try to read both stdoutput and stderr at the same time!! There problem is clearly explained at http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.redirectstandardoutput(VS.80).aspx

The document suggests to create threads to handle this issue or alternatively use OutputDataReceived event, and so I did:

For complete source code listing please refer to the attachment

 Hope this helps

2 Comments

Comments have been disabled for this content.