title: How to identify that Process is complete now, end close this event OutputDataReceived in C#
body: <p>I'm using <code>Process</code> of C# for open <code>cmd</code> and fire command. After completion of this process I want to use List which one is filled by <code>OutputDataReceived</code> for further use. </p>
  
  <p>But, i couldn't be able to identify that <code>Process</code> is completes now and you can use List. </p>
  
  <p>I'm getting this error. <code>collection-was-modified-enumeration-operation-may-not-execute</code></p>
  
  <p><strong>Code</strong></p>
  
  <pre><code>        String exeFileName = "cmd";
          List&lt;string&gt; list = new List&lt;string&gt;();
          ProcessStartInfo info = new ProcessStartInfo(exeFileName);
  
          info.Arguments = @"/c myCommand";
  
          info.RedirectStandardOutput = true;
          info.RedirectStandardError = true;
          info.UseShellExecute = false;
  
          Process proc = new Process();
          proc.StartInfo = info;
  
          // Set our event handler to asynchronously read the sort output.
          proc.OutputDataReceived += new DataReceivedEventHandler(proc_OutputDataReceived);
          proc.ErrorDataReceived += new DataReceivedEventHandler(proc_ErrorDataReceived);
  
          try
          {
  
              proc.Start();
              proc.BeginErrorReadLine();
              proc.BeginOutputReadLine();
              // i don't want to pass some time interval here like WaitForExit(1000)
              // cause command takes dynamic time
              proc.WaitForExit(); 
              if (!proc.HasExited)
              {
                  proc.Kill();
              }
  
              // this throws error like "collection-was-modified-enumeration-operation-may-not-execute"
              foreach(i in list)
              {
                  //logic
              }               
          }
          catch (Exception ex)
          {
              throw;
          }
      }
  
      static void proc_ErrorDataReceived(object sender, DataReceivedEventArgs e)
      {
          if (e.Data != null)
          {
             // logic
             list.Add(e.data);
          }
      }
  
      static void proc_OutputDataReceived(object sender, DataReceivedEventArgs e)
      {
         // logic
         list.Add(e.data);
      }
  </code></pre>