OutputDataReceived没有实时输出

时间:2014-02-14 09:37:04

标签: c#

我正在使用cmd和cmd至少需要10秒才能完成所有工作。 和cmd大约每秒抛出一行,我想同时读取该行。 但我不知道我的代码中有什么错误,它会在进程结束后抛出所有cmd行。 我希望有人能帮助我摆脱这个问题。 :) //方法

private void readBack()
        {
            var process = new Process();
            process.StartInfo.FileName = "XXXX.exe";
            process.StartInfo.Arguments = "XXXXXXX";
            process.StartInfo.CreateNoWindow = true;
            process.StartInfo.UseShellExecute = false;
            process.StartInfo.RedirectStandardOutput = true;
            process.OutputDataReceived += new DataReceivedEventHandler(process_OutputDataReceived);
            process.Start();
            process.BeginOutputReadLine();
            //process.WaitForExit();
        }

//输出数据

private void process_OutputDataReceived(object sender, DataReceivedEventArgs e)
        {
             if (!string.IsNullOrEmpty(e.data))
        {
            if (InvokeRequired)
            {
                Action action = () => richTextBox1.AppendText(e.data + "\r\n");
                richTextBox1.Invoke(action);
            }
            else
            {
                richTextBox1.AppendText(e.data + "\r\n");
            }
        }
        }

// btn code

private void button1_Click(object sender, EventArgs e)
    {
        BackgroundWorker bW = new BackgroundWorker();
        bW.DoWork += (s, eb) =>
            {
                readBack();
            };
        bW.RunWorkerAsync();
    }

0 个答案:

没有答案