I wrote a process that reads data from a file specified as an argument. I read StandardOutput asynchronously and StandardError synchronously.
public static string ProcessScript(string command, string arguments)
{
Process proc = new Process();
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.RedirectStandardError = true;
proc.StartInfo.FileName = command;
proc.StartInfo.Arguments = arguments;
proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
proc.Start();
string error = null;
string output = null;
proc.OutputDataReceived += (sender, outputLine) =>
{
if (outputLine.Data != null)
{
output += outputLine.Data;
}
};
proc.BeginOutputReadLine();
error = proc.StandardError.ReadToEnd();
proc.WaitForExit();
proc.Close();
return output;
}
After the process is complete, I get the output. But not really. I receive only partial data. Asynchronous reading is not completed even after the process has completed, so I only get partial data. I need the full line that is specified.
Edit:
I am using .Net 3.5. I can not use ReadToEndAsyncMethod
Any ideas?
source
share