title: Backgroundworker stays busy
body: <p>In my app I have a picturebox and 2 buttons("Yes" and "No"). Yes adds 1 to the resultlist, No adds 0 and both go to the next picture.
  Now I need to implement a timer in the app which makes the picture go to the next if no answer supplied. I thought to use a backgroundworker for it.</p>
  
  <p>Code below switches the pictures fine when I don't click a button. Clicking a button freezes the UI because the backgroundworker stays "Busy".
  I do understand that CancelAsync does not stop the backgroundworker immediatelly, but the return-statement in DoWork is actually hit.</p>
  
  <p>So my question here is why does the backgroundworker stay busy or am I totally going the wrong way here?</p>
  
  <pre><code>    protected override void OnLoad(EventArgs e)
      {
          base.OnLoad(e);
          Counter = 0;
  
          _backgroundWorker = new BackgroundWorker();
          _backgroundWorker.DoWork += _backgroundWorker_DoWork;
          _backgroundWorker.WorkerSupportsCancellation = true;
          _backgroundWorker.RunWorkerAsync();
      }
  
      private void _backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
      {
          BackgroundWorker bgw = sender as BackgroundWorker;
  
          GoToNextItem(); //Show next picture
          while (!bgw.CancellationPending) 
          {
              _getNext = false;
              Stopwatch sw = Stopwatch.StartNew();
  
              //Wait interval-time
              while (!_getNext)
              {
                   if ((sw.ElapsedMilliseconds &gt; Test.Interval * 1000) &amp;&amp; !bgw.CancellationPending)
                  {
                      _getNext = true;
                  }
                  if (bgw.CancellationPending)
                  {
                      e.Cancel = true;
                      return; //Breakpoint is hit here
                  }
              }
              if (_getNext)
              {
                 Result.Add(0);
                  GoToNextItem();
              }
          }
          e.Cancel = true;
      }
  
      private void btnNo_Click(object sender, EventArgs e)
      {
          _backgroundWorker.CancelAsync();
          Result.Add(0);
  
          while (_backgroundWorker.IsBusy)
          {
          _backgroundWorker.CancelAsync();
              System.Threading.Thread.Sleep(20);
          }
          _backgroundWorker.RunWorkerAsync();
      }
  
      private void btnYes_Click(object sender, EventArgs e)
      {
          _backgroundWorker.CancelAsync();
          Result.Add(1);
  
          while (_backgroundWorker.IsBusy) //Stays busy ==&gt; UI freezes here
          {
              _backgroundWorker.CancelAsync();
              System.Threading.Thread.Sleep(20);
          }
          _backgroundWorker.RunWorkerAsync();
      }
  </code></pre>
  
  <p><strong>Edit</strong></p>
  
  <p>Changed the code by using a Timer as suggested by @Servy.
  For more elaboration about the backgroundworker-question, read the comments of the accepted answer.</p>