title: How to stop BackgroundWorker on Form's Closing event to avoid RaceOnRCWCleanup error and to aovid form1 dispose before backgroundworker finish?
body: <p>In Form1 top i added this:</p>
  
  <pre><code>bool completed;
  AutoResetEvent autoreset;
  </code></pre>
  
  <p>In the constructor i did:</p>
  
  <pre><code>completed = false;
  autoreset = new AutoResetEvent(false);
  </code></pre>
  
  <p>In the backgroundworker DoWork event i did:</p>
  
  <pre><code>private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
          {
              BackgroundWorker worker = sender as BackgroundWorker;
              while (true)
              {
                  completed = true;
                  if ((worker.CancellationPending == true))
                  {
                      e.Cancel = true;
                      break;
                  }
                  else
                  {
                      if (tempCpuValue &gt;= (float?)nud1.Value || tempGpuValue &gt;= (float?)nud1.Value)
                      {
                          soundPlay = true;
                          NudgeMe();
                      }
                      else
                      {
                          soundPlay = false;
                          stop_alarm = true;
  
                      }
  
                      tempCpuValue = Core.cpuView(pauseContinueDoWork,cpu,this,data,tempCpuValue,button1);
                      tempGpuValue = Core.gpuView(pauseContinueDoWork,data,tempGpuValue,button1);
                      this.Invoke(new Action(() =&gt; data = new List&lt;string&gt;()));
                      tempCpuValue = Core.cpuView(pauseContinueDoWork, cpu, this, data, tempCpuValue, button1);
                      tempGpuValue = Core.gpuView(pauseContinueDoWork, data, tempGpuValue, button1);
                      this.Invoke(new Action(() =&gt; listBox1.DataSource = null));
                      this.Invoke(new Action(() =&gt; listBox1.DataSource = data));
  
  
  
                      //listBox1.DataSource = data;
  
                  }
              }
              autoreset.Set();
          }
  </code></pre>
  
  <p>Then in the Form1 closing event i did:</p>
  
  <pre><code>private void Form1_FormClosing(object sender, FormClosingEventArgs e)
          {
              if (MessageBox.Show("Are you Sure you want to Exit. Click Yes to Confirm and No to continue", "WinForm", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
              {
                  e.Cancel = true;
              }
              else
              {
                  if (completed == true)
                  {
                      this.backgroundWorker1.CancelAsync();
                      autoreset.WaitOne();
                  }
              }
  
  
          }
  </code></pre>
  
  <p>When it's doing the WaitOne() the form just back show up in the middle and the program is freezing all i can do is in the visual studio Debug>Stop Debugging</p>
  
  <p>I wanted to do it to aovid exceptions im getting sometimes RaceOnRCWCleanup something about multi threadings.</p>
  
  <p>And sometimes when closing the program i got on another class this exception:</p>
  
  <pre><code>System.ObjectDisposedException was unhandled by user code
    HResult=-2146232798
    Message=Cannot access a disposed object.
  Object name: 'Form1'.
    Source=System.Windows.Forms
    ObjectName=Form1
    StackTrace:
         at System.Windows.Forms.Control.MarshaledInvoke(Control caller, Delegate method, Object[] args, Boolean synchronous)
         at System.Windows.Forms.Control.Invoke(Delegate method, Object[] args)
         at System.Windows.Forms.Control.Invoke(Delegate method)
         at HardwareMonitoring.Core.cpuView(Boolean pause, CpuTemperature cpuTemp, Form1 f1, List`1 myData, Nullable`1 myCpuTemp, Button b1) in d:\C-Sharp\HardwareMonitoring\HardwareMonitoring\Hardwaremonitoring\Core.cs:line 55
         at HardwareMonitoring.Form1.backgroundWorker1_DoWork(Object sender, DoWorkEventArgs e) in d:\C-Sharp\HardwareMonitoring\HardwareMonitoring\Hardwaremonitoring\Form1.cs:line 427
         at System.ComponentModel.BackgroundWorker.OnDoWork(DoWorkEventArgs e)
         at System.ComponentModel.BackgroundWorker.WorkerThreadStart(Object argument)
    InnerException: 
  </code></pre>
  
  <p>Like the form1 disposed before the backgroundworker finished the job and the backgrounddworker dowork event is using this class methods.</p>