title: Manupilating Process Output in Win Forms (C#)
body: <p>I have a WinForms application that runs multiple processes which run as background workers. I create a background worker for each new Process</p>
  
  <pre><code>BackgroundWorker background = new BackgroundWorker();
  background.DoWork += new DoWorkEventHandler(bkWorker_DoWork);
  background.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bkWorker_Complete);
  background.WorkerReportsProgress = false;
  background.WorkerSupportsCancellation = true;
  </code></pre>
  
  <p>My Dowork code looks like this:</p>
  
  <pre><code>private void bkWorker_DoWork(object sender, DoWorkEventArgs e) {
    WorkerArgument obj = (WorkerArgument) e.Argument;
    BackgroundWorker worker = (BackgroundWorker) sender;
  
    Process proc = new Process();
    proc.StartInfo.FileName = "cmdprocess"; //"cmd.exe";
    proc.StartInfo.UseShellExecute = false;
    proc.StartInfo.CreateNoWindow = true;
    proc.StartInfo.RedirectStandardOutput = true;
    proc.StartInfo.RedirectStandardInput = true;
    proc.StartInfo.RedirectStandardError = true;
  
    proc.OutputDataReceived += new DataReceivedEventHandler(SortOutputHandler);
  
    proc.ErrorDataReceived += new DataReceivedEventHandler(ErrorOutputHandler);
  
    proc.Start();
  
    process.Add(obj.Row, proc.Id);
    proc.BeginOutputReadLine();
    proc.BeginErrorReadLine();
    proc.WaitForExit();
    proc.Close();
  }
  </code></pre>
  
  <p>As you may notice that I am using an event handler to sort the output given by the cmdProcess (SortOutputHandler) same as Error output.</p>
  
  <p>I am showing this output in a text box</p>
  
  <pre><code> private void SortOutputHandler(object sendingProcess, DataReceivedEventArgs outLine) {
     if (!String.IsNullOrEmpty(outLine.Data)) {
       if (txtLog.InvokeRequired) {
         this.Invoke((MethodInvoker) delegate {
           string[] lines = txtLog.Lines;
           if (lines.Length &gt; 200) {
             string[] newlines = lines.Skip(200) as string[];
             txtLog.Lines = newlines;
           }
           txtLog.AppendText(Environment.NewLine + outLine.Data);
         });
       } else {
         string[] lines = txtLog.Lines;
         if (lines.Length &gt; 200) {
           string[] newlines = lines.Skip(200) as string[];
           txtLog.Lines = newlines;
         }
         txtLog.AppendText(Environment.NewLine + outLine.Data);
       }
  
  
     }
   }
  </code></pre>
  
  <p>The Error Handler is as follows:</p>
  
  <pre><code>private void ErrorOutputHandler(object sendingProcess, DataReceivedEventArgs outLine) {
    if (!String.IsNullOrEmpty(outLine.Data)) {
      if (txtLog.InvokeRequired) {
        this.Invoke((MethodInvoker) delegate {
          string[] lines = txtLog.Lines;
          if (lines.Length &gt; 200) {
            string[] newlines = lines.Skip(200) as string[];
            txtLog.Lines = newlines;
          }
          txtLog.AppendText(Environment.NewLine + outLine.Data);
  
        });
      } else {
        string[] lines = txtLog.Lines;
        if (lines.Length &gt; 200) {
          string[] newlines = lines.Skip(200) as string[];
          txtLog.Lines = newlines;
        }
        txtLog.AppendText(Environment.NewLine + outLine.Data);
      }
  
    }
  }
  </code></pre>
  
  <p>The problem is that when the number of processes increases the log starts running rapidly. </p>
  
  <p>I have created a new Form frmLog with a multi-line textbox inside it, Can you tell me How I can use it to get Output from one process? Like there can be a "View Log" Button and when I click it only the log of that process show.</p>
  
  <p>Also is it possible to write the output of all the processes to unique files separately?</p>