I have written a Process which reads data from the file given as an argument. I have read the 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();
//I have not got entire Output
return output;
}
After the process has been finished I am getting output. But not entirely. I am getting only partial data. The asynchronous reading is not over even after the process completes its task, So only I am getting partial data. I need complete string which is given.
Edit:
I am using .Net 3.5. I can't use ReadToEndAsync
Method
Any ideas?