接收到来自process.OutputDataReceived的重复输出

接收到来自process.OutputDataReceived的重复输出,process,io-redirection,redirectstandardoutput,Process,Io Redirection,Redirectstandardoutput,我对重定向输出中的重复内容有问题 在我的表单中有两个按钮:运行和清除 public partial class BatchRun : Form { Process process = new Process(); public BatchRun() { InitializeComponent(); } private void RunBTN_Click(object sender, Ev

我对重定向输出中的重复内容有问题

在我的表单中有两个按钮:运行和清除

  public partial class BatchRun : Form
 {
  Process process = new Process();

   public BatchRun()
    {
        InitializeComponent();                         
    }

    private void RunBTN_Click(object sender, EventArgs e)
    {
        //initiate the process 
        this.process.StartInfo.FileName = sMasterBATname;
        this.process.StartInfo.UseShellExecute = false;
        this.process.StartInfo.CreateNoWindow = true;
        this.process.StartInfo.RedirectStandardOutput = true;
        this.process.OutputDataReceived += new DataReceivedEventHandler(StandardOutputHandler);
        this.process.StartInfo.RedirectStandardInput = true;
        this.process.Start();
        this.process.BeginOutputReadLine();
    }

public void StandardOutputHandler(object sender, DataReceivedEventArgs outLine)
    {
        BeginInvoke(new MethodInvoker(() =>
                {
                    Label TestLBL = new Label();
                    TestLBL.Text = text.TrimStart();
                    TestLBL.AutoSize = true;
                    TestLBL.Location = new Point(10, CMDpanel.AutoScrollPosition.Y + CMDpanel.Controls.Count * 20);
                    CMDpanel.Controls.Add(TestLBL);
                    CMDpanel.AutoScrollPosition = new Point(10, CMDpanel.Controls.Count * 20);
                }));            
    }


private void ClearBTN_Click(object sender, EventArgs e)
    {           
        CMDpanel.Controls.Clear();                       
        this.process.CancelOutputRead();
        this.process.Close();
        this.process.Refresh();                                   
    }
 }
如果我只想运行一次流程(即流程完成后关闭表单),这将非常有效

但是,我需要允许用户重新运行相同的进程或运行新的进程,因此我添加了一个清除按钮,清除各种控件等

我遇到的问题是,在单击“清除”按钮后,我想再次单击“运行”按钮而不关闭该按钮,然后运行sMAsterBAT文件(CMD)

StandardOutputHandler似乎包含了上一次运行的内容以及新运行的内容,从而在我的CMDpanel中产生了重复的标签

这是否存储在某种缓冲区中?如果是,如何清除它以允许重新运行


请有人解释一下为什么会发生这种情况,以及如何解决它

和工作中为我修好的人谈过。太容易了,哈哈

 private void ClearBTN_Click(object sender, EventArgs e)
  {           
    CMDpanel.Controls.Clear();                       
    this.process.CancelOutputRead();
    this.process.Close();
    this.process.Refresh();  
    this.process = new Process();  // this line resolved my issue!!                                 
   }

}

没有人知道或能够提供帮助???绝对没有建议?或者回答为什么会发生?????