1

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?

Buddhika Bandara
  • 351
  • 1
  • 11
  • 3
    [How do I get output from a command to appear in a control on a Form in real-time?](https://stackoverflow.com/a/51682585/7444103). Read the notes about the `SynchronizingObject` (to also avoid to `BeginInvoke()`). – Jimi May 12 '20 at 12:29

0 Answers0