locked
How to stop background worker? RRS feed

  • Question

  • I wanted to make a website checker and i got this part of code:

    private void button1_Click(object sender, EventArgs e)
            {
                
                if (radioButton1.Checked)
                {
                    try
                    {
                        if (!checkURL("http://" + textBox1.Text))
                        {
                            MessageBox.Show("Error!");
                            return;
                        }
                    }
                    catch
                    {
                        MessageBox.Show("Error");
                        return;
                    }
                }
                else
                {
                    try
                    {
                        var ping = new Ping();
                        var aa = ping.Send(IPAddress.Parse(textBox2.Text));
                        if (aa.Status != IPStatus.Success)
                        {
                            MessageBox.Show("Error!");
                            return;
                        }
                    }
                    catch
                    {
                        MessageBox.Show("Error");
                        return;
                    }
                }
                time = DateTime.Now;
                for (var i = 0; i < Environment.ProcessorCount * 2; i++)
                {
                    //var bw = new BackgroundWorker();
                    bw = new BackgroundWorker();
                    bw.DoWork += (s, a) => a1(radioButton2.Checked);
                    bw.RunWorkerAsync();
                }
    
              //  var t = new Timer();
                t.Interval = 25;
                t.Tick += (s, a) =>
                    {
                       
                        label2.Text = i.ToString();
                    };
                t.Start();
    
              //  var t1 = new Timer();
                t1.Interval = 1000;
                t1.Tick += (s, a) =>
                {
                   
                    label6.Text = (i - i1).ToString(); 
                    label8.Text = ((int)DateTime.Now.Subtract(time).TotalSeconds).ToString();
                    i1 = i;
                };
                t1.Start();
            }
    

    How can i stop background worker on button 2 click?

    Monday, September 3, 2012 1:42 AM

Answers

  • As per your code, the Button 2 method should have access to same bw (background worker) object/variable, that was created in button1 click.

    Use CancelAsync() method to request cancellation.

    Check the Link above for code sample.


    .NET Maniac -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

    • Proposed as answer by Jason Dot Wang Tuesday, September 4, 2012 8:12 AM
    • Marked as answer by Jason Dot Wang Monday, September 10, 2012 8:20 AM
    Monday, September 3, 2012 4:37 AM
  • create a cancel button and add CancelAsync() to cancel.. or you can add a method to cancel background worker, use loop or anything like when some operation reach to some limit, then call or execute this CancelAsync() method.
    • Proposed as answer by Jason Dot Wang Tuesday, September 4, 2012 8:12 AM
    • Marked as answer by Jason Dot Wang Monday, September 10, 2012 8:20 AM
    Monday, September 3, 2012 5:02 AM
  • In your DoWork method, check for BackgroundWorker.CancellationPending true, set e.Cancel to true and exit the DoWork method.  Call BackgroundWorker.CancelAsync to set CancellationPending.
    • Proposed as answer by Jason Dot Wang Tuesday, September 4, 2012 8:12 AM
    • Marked as answer by Jason Dot Wang Monday, September 10, 2012 8:20 AM
    Monday, September 3, 2012 6:15 AM

All replies

  • As per your code, the Button 2 method should have access to same bw (background worker) object/variable, that was created in button1 click.

    Use CancelAsync() method to request cancellation.

    Check the Link above for code sample.


    .NET Maniac -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

    • Proposed as answer by Jason Dot Wang Tuesday, September 4, 2012 8:12 AM
    • Marked as answer by Jason Dot Wang Monday, September 10, 2012 8:20 AM
    Monday, September 3, 2012 4:37 AM
  • create a cancel button and add CancelAsync() to cancel.. or you can add a method to cancel background worker, use loop or anything like when some operation reach to some limit, then call or execute this CancelAsync() method.
    • Proposed as answer by Jason Dot Wang Tuesday, September 4, 2012 8:12 AM
    • Marked as answer by Jason Dot Wang Monday, September 10, 2012 8:20 AM
    Monday, September 3, 2012 5:02 AM
  • In your DoWork method, check for BackgroundWorker.CancellationPending true, set e.Cancel to true and exit the DoWork method.  Call BackgroundWorker.CancelAsync to set CancellationPending.
    • Proposed as answer by Jason Dot Wang Tuesday, September 4, 2012 8:12 AM
    • Marked as answer by Jason Dot Wang Monday, September 10, 2012 8:20 AM
    Monday, September 3, 2012 6:15 AM