0

I'm having an issue with duplicated content from redirected output.

In my forms I have two buttons: Run and Clear.

  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();                                   
    }
 }

This works great if I want to run process only once i.e. close the forms once process has completed.

However, I need to allow user to rerun the same process or run a new one hence I have added a clear button the clear various controls etc.

The problem I'm having is that after clicking clear button, I want to click run button again without closing which should then run the sMAsterBAT file(CMD).

StandardOutputHandler seems to be including content of the previous run as well as the new one resulting in duplicated labels in my CMDpanel.

Is this stored in some kind of buffer? If so, How do i clear it to allow me a rerun?

Could someone explain why this is happening and how to resolve it please.

2
  • No one knows or able to help???? Feb 9 2017 at 20:20
  • Absolutely no suggestions? or answer as to why it happens????? Feb 10 2017 at 10:52
0

Spoke to someone at work who fixed it for me. So easy lol

 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!!                                 
   }

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.