简体   繁体   English

从启动的过程中逐行获取输出

[英]Getting line by line output from a started process

I'm trying to retrieve the output lines generated by a process I started, here's the code 我正在尝试检索由我启动的进程生成的输出行,这是代码

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
    foreach (myData singleJob in e.Argument as List<myData>)
    {
        ProcessStartInfo psi = new ProcessStartInfo("myCommandLineProgram.exe");
        psi.Arguments = "\"" + singleJob.row + "\"";
        psi.CreateNoWindow = true;
        psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
        psi.RedirectStandardInput = true;
        psi.RedirectStandardOutput = true;
        psi.RedirectStandardError = true;
        psi.UseShellExecute = false;
        Process p = new Process();
        p.StartInfo = psi;
        p.Start();
        StreamReader sr = p.StandardOutput ;
        string line;
        while ((line = sr.ReadLine()) != null )
        {
            this.Invoke((MethodInvoker)delegate
            {
                    richTextBox1.AppendText(sr.ReadLine() + Environment.NewLine);
                    richTextBox1.ScrollToCaret();   

            });
        }

        //StreamReader streamOutput = p.StandardOutput;
        //string content = streamOutput.ReadToEnd();   
        //this.Invoke((MethodInvoker)delegate
        //{
        //    richTextBox1.AppendText(content + Environment.NewLine);
        //});

        p.WaitForExit();
    }
}

While the commented out code is always working (but it doesn't parse line by line), the code above is somehow faulty, indeed some lines fail to appear into the richtextbox and some others are blank. 尽管注释掉的代码始终有效(但是不能逐行解析),但是上面的代码还是有问题的,确实有些行无法显示在richtextbox中,而另一些则为空白。

Thanks 谢谢

Shouldn't that be something like 那不应该像

richTextBox1.AppendText(line + Environment.NewLine);

( line instead of sr.ReadLine() )? line而不是sr.ReadLine() )?

Calling readLine() twice will discard every second line. 两次调用readLine()将每隔第二行丢弃一次。

Also, since you're calling ReadLine in the delegate you cannot control when the read occurs. 另外,由于您在委托中调用ReadLine ,因此无法控制何时进行读取。 It might be that there were several ReadLines() (from the while line) in between. 之间可能有多个ReadLines() (来自while行)。

Note, that you should also NOT use the line variable: This variable always references the same line variable in the loop, this one might contain new content at the time the AppendText is executed. 请注意,您也不应使用line变量:此变量在循环中始终引用同一行变量,在执行AppendText时此变量可能包含新内容。 You should introduce a new local variable inside the loop like 您应该在循环内引入一个新的局部变量,例如

 while ((line = sr.ReadLine()) != null )
 {
   var theLine = line;
   this.Invoke((MethodInvoker)delegate
   {
       richTextBox1.AppendText(theLine + Environment.NewLine);
       richTextBox1.ScrollToCaret();   
   });
 }

Just change here instead of ReadLine() , put line . 只是在这里更改而不是ReadLine() ,将line放进去。 You have already read the line in your while loop 您已经在while循环中阅读了该行

string appendingLine = line;
this.Invoke((MethodInvoker)delegate
{
          richTextBox1.AppendText(appendingLine + Environment.NewLine);
          richTextBox1.ScrollToCaret();   

});

EDIT: The answer MartinStettner gave is better one. 编辑: MartinStettner给的答案是更好的选择。 There may be the case where line changes before the delegate is executed, therefore some lines can be lost while others can be repeated. 可能存在在执行委托之前更改line的情况,因此某些行可能会丢失,而其他行可能会重复。 Therefore I will change my answer according to Martin's and I want to point out that he should be the one who will take the credit for this answer. 因此,我将根据马丁的回答更改答案,我想指出的是,他应该是这一答案的功臣。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM