title: How can I restart a backgroundworker in a Windows Forms Application?
body: <p>What my code basically does is it allows the user to browse for a folder that has a bunch of files, and when the user clicks a &quot;Start&quot; button, a code that makes files with the same name and different extensions for the ones in the folder runs. What I want to do is add a cancel button so if the user clicks the start button on a folder that was the incorrect one for example, the user can cancel out, browse to the correct folder, and restart the operation.
  This is the code that fires when the start button is clicked:</p>
  <pre><code>class ExcelCode
  {
      public static void DoExcel (string FolderPath)
      {
          System.IO.StreamWriter file = new System.IO.StreamWriter(FolderPath + &quot;.fdf&quot;);
          Thread.Sleep(1000);
      }
  }
  </code></pre>
  <p>What I tried so far after hours upon hours of researching was to have the <code>backgroundworker.CancelAsync();</code> called on the Cancel button, and a global variable that keeps track of the cancellation, ie. the DoWork() method looks like this:</p>
  <pre><code>private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
      {
          if(backgroundWorker1.IsBusy)
          for (int i = 0; i &lt; 100; i++)
          {
              foreach (string file in filePaths)
              {
                  if(backgroundWorker1.CancellationPending)
                  {
                      e.Cancel = true;
                      restartWorker = true;
                      return;
                  }
                  ExcelCode.DoExcel(file);
                  //write to textbox
              }
              backgroundWorker1.ReportProgress(i);
          }
      }
  </code></pre>
  <p>And my RunWorkerComplete method looks like this:</p>
  <pre><code>private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
      {
          if (e.Cancelled &amp;&amp; restartWorker)
          {
              if (backgroundWorker1.IsBusy)
              {
                  restartWorker = true;
                  backgroundWorker1.CancelAsync();
                  return;
              }
              MessageBox.Show(&quot;Worker has cancelled&quot;);
              restartWorker = false;
              return;
          }
      }
  </code></pre>
  <p>Start Button:</p>
  <pre><code>private void button1_Click(object sender, EventArgs e)
      {
          if (backgroundWorker1.IsBusy)
          {
              restartWorker = true;
              backgroundWorker1.CancelAsync();
          }
          else
          {
              backgroundWorker1.RunWorkerAsync();
          }
          button2.Enabled = true;
      }
  </code></pre>
  <p>Cancel Button:</p>
  <pre><code>  private void button2_Click(object sender, EventArgs e)
      {
          backgroundWorker1.CancelAsync();
      }
  </code></pre>
  <p>Now this all works well for cancelling out just once, when I try to start again on a different folder I get this error:</p>
  <blockquote>
  <p>An exception of type 'System.IO.IOException' occurred in mscorlib.dll but was not handled in user code</p>
  <p>Additional information: The process cannot access the file 'C:\Users\Pierre\Desktop\Test Folder - Copy\New Text Document - Copy - Copy - Copy - Copy - Copy (8).txt.fdf' because it is being used by another process.</p>
  </blockquote>
  <p>The code has everything I researched on so it should pretty much show my entire progress on this. Any guidance as to what should be done is greatly appreciated.</p>
  <p>EDIT: The code actually works for the second folder, that is it generates the file but throws the exception anyway.</p>