title: Why am I getting this InvalidOperationException in this C# wpf application?
body: <p>I have a C# (WPF) program that searches through files in a directory, and I wanted a progress bar to show my progress searching through the files in the directory (since there could be thousands of files in a directory).  </p>
  
  <p>I decided to use background workers so that the progress bar updates correctly (which isn't happening, but that's not my biggest problem).  </p>
  
  <p>Currently what happens is it SOMETIMES works which is super weird, but most times I get this error.  The only time I don't get this error is if I add a <code>MessageBox.Show();</code> in the <code>foreach(document in myDirectory)</code> loop to slow it down and view what number file it's on.  Then it only errors out every so often.  </p>
  
  <p>Can anyone explain to me why I'm getting this error, and how to fix it?  Also, if you think there's a better method to show the progress bar, please don't hesitate to correct what I'm doing.</p>
  
  <p><strong>My error</strong></p>
  
  <blockquote>
    <p>An exception of type 'System.InvalidOperationException' occurred in System.dll but was not handled in user code</p>
    
    <p>Additional information: This operation has already had OperationCompleted called on it and further calls are illegal.</p>
  </blockquote>
  
  <p><strong>myApp.xaml.cs</strong></p>
  
  <pre><code>BackgroundWorker backgroundWorker1;
  
  public void searchFiles()
  {
      //50 files in the directory
      int fileCount = 50;
      int documentCounter = 0;
      //For every file in the directory
      foreach(document in myDirectory)
      {
      //increment the file count
      documentCounter++;
      FileSearchProgressBar(documentCounter, fileCount);
      }
  }
  
  private void FileSearchProgressBar(int currentDocNumber, int count)
  {
      backgroundWorker1 = new BackgroundWorker();
      backgroundWorker1.WorkerReportsProgress = true;
      backgroundWorker1.DoWork += (obj, ea) =&gt; CalculateProgress(currentDocNumber, count);
      backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler(progressReport);
      backgroundWorker1.RunWorkerAsync();
  }
  
  void progressReport(object sender, ProgressChangedEventArgs e)
  {
      progBar.Value = e.ProgressPercentage;
  }
  
  private void CalculateProgress(int currentDocNumber, int count)
  {
      //This is where the error is reporting
      backgroundWorker1.ReportProgress(currentDocNumber * (100 / count));
  }
  </code></pre>
  
  <p><strong>EDIT1</strong></p>
  
  <p><strong>Stack Trace</strong></p>
  
  <p><code>
  System.InvalidOperationException: This operation has already had OperationCompleted called on it and further calls are illegal.
    at System.ComponentModel.AsyncOperation.VerifyNotCompleted()
    at System.ComponentModel.AsyncOperation.Post(SendOrPostCallback d, Object arg)
    at System.ComponentModel.BackgroundWorker.ReportProgress(Int32 percentProgress, Object userState)
    at System.ComponentModel.BackgroundWorker.ReportProgress(Int32 percentProgress)
    at HR_Search_Program.MainWindow.CalculateProgress(Int32 currentDocNumber, Int32 count) in c:\Users\myname\Documents\Visual Studio 2013\Projects\myproject\myproject\MainWindow.xaml.cs:line 589
  </code></p>