0

In my form, I start things by running an executable file loader.exe(another project in visual studio) which prints some information on console over time until it terminates. What I want to do is: I want to read console while it continues executing and display the latest output(percentage with some extra info) in a text box textBoxConsole1 in my form application so that the user can be aware of the progress.

EDIT: Currently, it is a bit complicated. It displays some of the output, then displays additional output and then displays the whole remaining output. Not as same as the loader.exe does.

In this thread C# Show output of Process in real time

Mr.Passant said:

"This is pretty normal, the process will switch to buffered output when you redirect its output. If it doesn't spit out a lot a text then that buffer doesn't fill up enough to cause it to be flushed. Nothing you can do about it if you can't fix the program's code."

So how much is this "enough" exactly? My Code:

private void buttonConnect_Click(object sender, EventArgs e)
    {
        Thread findThread = new Thread(findProcedure);
        findThread.Start();
    }

    public void findProcedure()
    {
        Process process = new Process();
        process.StartInfo.FileName = PATH;
        process.StartInfo.UseShellExecute = false;
        process.StartInfo.RedirectStandardOutput = true;
        process.StartInfo.RedirectStandardError = true;
        process.StartInfo.RedirectStandardInput = true;
        process.StartInfo.CreateNoWindow = true;

        process.OutputDataReceived += new DataReceivedEventHandler((sender, e) =>
        {
            if (!String.IsNullOrEmpty(e.Data))
            {
                //textBoxConsole1.Text = e.Data; //Cross-thread validation exception
                //use thread safe set method instead
                setConsole1(e.Data);
            }
        });


        process.ErrorDataReceived += new DataReceivedEventHandler((sender, e) =>
        {
            if (!String.IsNullOrEmpty(e.Data))
            {
                setConsole3(e.Data);
            }
        });

        process.Start();
        process.BeginOutputReadLine();
        process.BeginErrorReadLine();

        process.WaitForExit();
    }

And my thread-safe set method:

public void setConsole1(string str)
    {
        if (this.textBoxConsole1.InvokeRequired)
        {
            SetTextCallback d = new SetTextCallback(setConsole1);
            this.Invoke(d, new object[] { str });
        }
        else
        {
            textBoxConsole1.AppendText(str);
            textBoxConsole1.AppendText(Environment.NewLine);
        }
    }

Error data handler method setConsole3 is the same as setConsole1 but sets to another box.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Arda Aras
  • 1
  • 3
  • Hello, welcome to SO, can we share some code and then we can all see how it works and what can be wrong? do you get an error or something? See the [How to Ask](https://stackoverflow.com/help/how-to-ask) page for help clarifying this question. – rmjoia Sep 28 '17 at 13:59
  • Have a look at: [https://stackoverflow.com/questions/285760/how-to-spawn-a-process-and-capture-its-stdout-in-net](https://stackoverflow.com/questions/285760/how-to-spawn-a-process-and-capture-its-stdout-in-net) – corners Sep 28 '17 at 14:01
  • @corners I can capture the output when process which produces it terminates but the problem is I want to capture it while the process uses it so that I can see the progress on my form – Arda Aras Sep 28 '17 at 14:11
  • Remove the WaitForExit – corners Sep 28 '17 at 14:42

1 Answers1

0

You should set RedirectStandardOutput to true in StartInfo.

Process process = new Process();
try
{
  process.StartInfo.FileName = fileName // Loader.exe in this case;
  ...
  //other startInfo props 
  ...
  process.StartInfo.RedirectStandardError = true;
  process.StartInfo.RedirectStandardOutput = true;
  process.OutputDataReceived += OutputReceivedHandler //OR (sender, e) => Console.WriteLine(e.Data);
  process.ErrorDataReceived += ErrorReceivedHandler;
  process.Start();
  process.BeginOutputReadline();
  process.BeginErrorReadLine();
  ....
  //other thing such as wait for exit 
}
farzaaaan
  • 410
  • 3
  • 9