I'm writing a program (a C# winforms application) to print the console output of a process to a Textbox as follows.
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;
}));
}
But when the program is running, It only prints the first line of the console output and nothing more. I have no idea about whats wrong with this code. Can anyone explain what's happening here?