title: How to implement Pause & Resume functionality with BackgroundWorker c#
body: <p>i want to implement pause resume and cancel functionality with BackgroundWorker. i have a one user control and all backgroundworker related code written in user control. i just add user control on flow layout control as many as time user click on buttons. i am fail to impelement
  <strong>Pause &amp; Resume functionality</strong> with <strong>ManualResetEvent</strong> but anyway i am not being able to do it. so here i am pasting my user control class related code and my form code.</p>
  
  <pre><code>public partial class ucBackgroundWorker : UserControl
  {
      System.ComponentModel.BackgroundWorker bgWorker = null;
      public event Action&lt;string, EventArgs&gt; Done;
      public event Action&lt;string, EventArgs&gt; Cancel;
      private static bool m_continue = true;
      private ManualResetEvent _resetEvent = new ManualResetEvent(false);
      //Semaphore sWaiter = new Semaphore(0, 1);
  
      public ucBackgroundWorker()
      {
          InitializeComponent();
          bgWorker = new System.ComponentModel.BackgroundWorker();
          bgWorker.WorkerSupportsCancellation = true;
          bgWorker.WorkerReportsProgress = true;
          bgWorker.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
          bgWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker1_RunWorkerCompleted);
          bgWorker.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged);
      }
  
      public void Run(int counter)
      {
          if (!bgWorker.IsBusy)
          {
              bgWorker.RunWorkerAsync(counter);
          }
          _resetEvent.Set();
      }
  
      private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
      {
          int input = int.Parse(e.Argument.ToString());
  
          this.BeginInvoke((MethodInvoker)delegate
          {
              lblStatus.Text = "Running";
          });
  
          for (int i = 1; i &lt;= input; i++)
          {
              _resetEvent.WaitOne();
              Thread.Sleep(500);
              (sender as System.ComponentModel.BackgroundWorker).ReportProgress(i * 10);
              if ((sender as System.ComponentModel.BackgroundWorker).CancellationPending)
              {
                  this.BeginInvoke((MethodInvoker)delegate
                  {
                      lblStatus.Text = "Cancel";
                  });
  
                  e.Cancel = true;
                  return;
              }
          }
          Thread.Sleep(1000);
      }
  
  
      // This event handler deals with the results of the 
      // background operation. 
      private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
      {
          // First, handle the case where an exception was thrown. 
          if (e.Error != null)
          {
              MessageBox.Show(e.Error.Message);
          }
          else if (e.Cancelled)
          {
              if (Cancel != null)
                  Cancel(this.Name, EventArgs.Empty);
          }
          else
          {
              this.BeginInvoke((MethodInvoker)delegate
              {
                  lblStatus.Text = "Done";
              });
  
              if (Done != null)
                  Done(this.Name, EventArgs.Empty);
          }
          _resetEvent.Reset();
      }
  
      // This event handler updates the progress bar. 
      private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
      {
          pBar.Refresh();
          pBar.Value = e.ProgressPercentage;
      }
  
      private void btnCancel_Click(object sender, EventArgs e)
      {
          if (bgWorker.IsBusy)
          {
              bgWorker.CancelAsync();
          }
      }
  
      private void btnPause_Click(object sender, EventArgs e)
      {
          if (bgWorker.IsBusy)
          {
              if (btnPause.Text.ToUpper() == "PAUSE")
              {
                  btnPause.Text = "Resume";
                  m_continue = false;
                  _resetEvent.Reset();
              }
              else if (btnPause.Text.ToUpper() == "RESUME")
              {
  
                  btnPause.Text = "Pause";
                  m_continue = true;
                  _resetEvent.Set();
  
              }
          }
      }
  }
  
      public partial class Form3 : Form
          {
              ucBackgroundWorker ucBgWorker = null;
  
              public Form3()
              {
                  InitializeComponent();
              }
  
              private void btnStart_Click(object sender, EventArgs e)
              {
                  ucBgWorker = new ucBackgroundWorker();
                  ucBgWorker.Done += new Action&lt;string, EventArgs&gt;(Worker_Done);
                  ucBgWorker.Cancel += new Action&lt;string, EventArgs&gt;(Worker_Cancel);
                  flowLayoutPanel1.Controls.Add(ucBgWorker);
                  ucBgWorker.Run(10);
              }
  
              void Worker_Done(string arg, EventArgs evtarg)
              {
                  label1.Text =arg + " Done One";
                  //System.Threading.Thread.Sleep(1000);
              }
  
              void Worker_Cancel(string arg, EventArgs evtarg)
              {
                  label1.Text = arg + " Cancel Click";
                  //System.Threading.Thread.Sleep(1000);
              }
  
          }
  </code></pre>
  
  <p>here i am attaching pic of mu UI how it looks.
  <img src="https://i.stack.imgur.com/DkMxm.png" alt="enter image description here"></p>