title: How to stop Backgroundworker and close the form?
body: <p>I want to completely stop the BackgroundWorker DoWork() Process while running for closing the form.</p>
  
  <p>I have applied following code but in  "this.Invoke" it throws error : "Invoke or BeginInvoke cannot be called on a control until the window handle has been created." while form close.</p>
  
  <pre><code>   private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
          {
              var dt_Images = db.Rings.Select(I =&gt; new { I.PaidRs, I.TypeID, I.RingID, I.CodeNo, Image = Image.FromStream(new MemoryStream(I.Image.ToArray())) }).OrderByDescending(r =&gt; r.TypeID);
              foreach (var dr in dt_Images.ThenByDescending(r =&gt; r.RingID).ToList())
              {
                  BTN = new Button();
                  BTN.TextImageRelation = TextImageRelation.TextAboveImage;
                  BTN.TextAlign = System.Drawing.ContentAlignment.BottomCenter;
                  BTN.AutoSize = true;
                  BTN.Name = dr.RingID.ToString();
                  BTN.Image = dr.Image;
                  BTN.Text = dr.CodeNo.ToString() + "    " + dr.TypeID.ToString();
                  this.Invoke(new MethodInvoker(delegate { if (backgroundWorker1 != null) flowLayoutPanel1.Controls.Add(BTN); else return; }));
                  BTN.Click += new EventHandler(this.pic_Click);
                  this.Invoke(new MethodInvoker(delegate { if (backgroundWorker1 == null) txt_pcs.Text = flowLayoutPanel1.Controls.Count.ToString(); else return;}));
              }
          }
  
    private void Form_KeyDown(object sender, KeyEventArgs e)
          {
              if (e.KeyData == Keys.Escape)
              {
                  backgroundWorker1.CancelAsync(); //backgroundworker doesnt stop here
                  backgroundWorker1 = null;   //it still invokes the delegate
                  this.Dispose();  
              }
          }
  </code></pre>
  
  <p>How to solved this error?</p>
  
  <p>please help me.</p>