Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C#: get external shell command result line by line

I am writing a C# winform application that starts a second process to execute shell commands like "dir" and "ping". I redirect the second process's output so my app can receive the command result. It roughly works fine.

The only problem is my winform app receives the command line output as a whole instead of line by line. For example, it has to wait for the external "ping" command to finish (which takes many seconds or longer) and then receives the whole output (many lines) at once.

What I want is the app receives the cmdline output in real-time, i.e. by lines not by block. Is this doable?

I am using this code to read the output: while ((result = proc.StandardOutput.ReadLine()) != null)

But it does not work the way I expected. Thanks in advance.

EDIT: here is the code I am using:

    System.Diagnostics.ProcessStartInfo procStartInfo = new 
            System.Diagnostics.ProcessStartInfo("cmd", "/c " + command);

    procStartInfo.WindowStyle = ProcessWindowStyle.Hidden;

    // The following commands are needed to redirect the standard output.
    procStartInfo.RedirectStandardOutput = true;
    procStartInfo.UseShellExecute = false;
    procStartInfo.CreateNoWindow = true;
    // Now we create a process, assign its ProcessStartInfo and start it
    System.Diagnostics.Process proc = new System.Diagnostics.Process();
    proc.StartInfo = procStartInfo;
    proc.Start();

    // Get the output into a string
    string result;
    try {
        while ((result = proc.StandardOutput.ReadLine()) != null)
        {
            AppendRtfText(result+"\n", Brushes.Black);
        }
    } // here I expect it to update the text box line by line in real time
      // but it does not.
like image 343
Charlie Avatar asked Aug 19 '11 12:08

Charlie


2 Answers

Have a look at the example in this msdn article on how to do the reading completly async.

Beyond that I expect your code does to read line by line now but the UI doesn't get any time to repaint (missing Application.DoEvents(); after updating the RTFTextBox

like image 88
Eddy Avatar answered Sep 30 '22 01:09

Eddy


Instead of loop using while ((result = proc.StandardOutput.ReadLine()) != null) you should of using:

...
proc.OutputDataReceived += proc_DataReceived;
proc.Start();
proc.BeginOutputReadLine();
proc.WaitForExit();

This will start asynchronous reading the lines when they arrives, you then handle the lines read by e.Data in proc_DataReceived handler, since you are use BeginOutputReadline the e.Data will be a string lines.

like image 30
Jalal Said Avatar answered Sep 30 '22 00:09

Jalal Said