Reputation: 337
I am trying to execute a console/batch application from a BackgroundWorker
in C# WPF application and show the output in a text box.
The batch application is as follows:
@echo off
echo start
"D:\openjdk-1.7.0-u40\bin\javac.exe"
echo finish
My C# code is:
Process process = new Process();
process.StartInfo.FileName = @"D:\exec.bat";
process.StartInfo.CreateNoWindow = true;
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardInput = true;
process.OutputDataReceived += new DataReceivedEventHandler(delegate(object sendingProcess, DataReceivedEventArgs outLine)
{
backgroundWorker.ReportProgress(0, System.Environment.NewLine + outLine.Data);
});
process.Start();
process.BeginOutputReadLine();
process.WaitForExit();
I have also tried using this to read the console stream:
string output = process.StandardOutput.ReadToEnd();
backgroundWorker.ReportProgress(0, System.Environment.NewLine + output);
In my output, it displays start
and finish
, but no output from javac.exe, whereas if I run the batch file myself from cmd.exe it does.
How can I make the output of javac.exe appear?
Upvotes: 0
Views: 872
Reputation: 337
After process.Start();
I added the following code to successfully produce an output:
do
{
string output = process.StandardOutput.ReadToEnd();
buildWorker.ReportProgress(0, System.Environment.NewLine + output);
}
while (!process.HasExited);
Upvotes: 2
Reputation:
Start javac.exe with parameter:
@echo off
echo start
start "D:\openjdk-1.7.0-u40\bin\javac.exe" /?
echo finish
Upvotes: 0