我正在编写一个程序(一个 C# winforms 应用程序)来将进程的控制台输出打印到文本框,如下所示。
private void button21_Click(object sender, EventArgs e)
{
Process p = new Process();
p.StartInfo.FileName = "node";
p.StartInfo.Arguments = "server.js";
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.WorkingDirectory = "D:/Dev/Workspace-JavaScript/p5js/SoftProject1/";
p.OutputDataReceived += new DataReceivedEventHandler(handler);
p.Start();
p.BeginOutputReadLine();
}
private void handler(object sender, DataReceivedEventArgs e)
{
Trace.WriteLine(e.Data);
this.BeginInvoke(new MethodInvoker(() =>
{
textBox1.Text += (e.Data ?? string.Empty)+Environment.NewLine;
}));
}
但是当程序运行时,它只打印控制台输出的第一行,仅此而已。我不知道这段代码有什么问题。谁能解释这里发生了什么?