C # - reading a standard byte of output byte from an external command line application

I am looking for a way to read a standard output byte by byte from an external command line application.

The current code works, but only when adding a new line. Is there a way to read the current byte of the byte input so that I can update the user with progress.

Ex)

Proccesing file

0% 10 20 30 40 50 60 70 80 90 100%

| ---- | ---- | ---- | ---- | ---- | ---- | ---- | ---- | ---- | ---- |

xxxxxx

A x is added every few minutes in a console application, but not in my C # Win form, as it only updates after the current line (end) is completed

Code example:

Process VSCmd = new Process();
VSCmd.StartInfo = new ProcessStartInfo("c:\\testapp.exe");
VSCmd.StartInfo.Arguments = "--action run"
VSCmd.StartInfo.RedirectStandardOutput = true;
VSCmd.StartInfo.RedirectStandardError = true;
VSCmd.StartInfo.UseShellExecute = false;
VSCmd.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
VSCmd.StartInfo.CreateNoWindow = true;

VSCmd.Start();

StreamReader sr = VSCmd.StandardOutput;

while ((s = sr.ReadLine()) != null)
{
   strLog += s + "\r\n";
   invoker.Invoke(updateCallback, new object[] { strLog });
}


VSCmd.WaitForExit();
+3
source share
2 answers

StreamReader.Read() , . int, -1, char .

+2

strLog , , updateCallback , , , .

  int currChar;
  int prevChar = 0;
  string strLog = string.Empty;
  while ((currChar = sr.Read()) != -1)
  {
    strLog += (char)currChar; // You should use a StringBuilder here!
    if (prevChar == 13 && currChar == 10) // Remove if you need to invoke for each char
    {
      invoker.Invoke(updateCallback, new object[] { strLog });
    }

    prevChar = currChar;
  }
0

Source: https://habr.com/ru/post/1745132/


All Articles