我正在尝试检索由我启动的进程生成的输出行,这是代码
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
foreach (myData singleJob in e.Argument as List<myData>)
{
ProcessStartInfo psi = new ProcessStartInfo("myCommandLineProgram.exe");
psi.Arguments = "\"" + singleJob.row + "\"";
psi.CreateNoWindow = true;
psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
psi.RedirectStandardInput = true;
psi.RedirectStandardOutput = true;
psi.RedirectStandardError = true;
psi.UseShellExecute = false;
Process p = new Process();
p.StartInfo = psi;
p.Start();
StreamReader sr = p.StandardOutput ;
string line;
while ((line = sr.ReadLine()) != null )
{
this.Invoke((MethodInvoker)delegate
{
richTextBox1.AppendText(sr.ReadLine() + Environment.NewLine);
richTextBox1.ScrollToCaret();
});
}
//StreamReader streamOutput = p.StandardOutput;
//string content = streamOutput.ReadToEnd();
//this.Invoke((MethodInvoker)delegate
//{
// richTextBox1.AppendText(content + Environment.NewLine);
//});
p.WaitForExit();
}
}
虽然注释掉的代码始终有效(但它不会逐行解析),但上面的代码有些错误,确实有些行未能出现在 Richtextbox 中,而另一些则为空白。
谢谢