title: Cancel a fuction/process
body: <p>I currently have a application that pings  a list of IPs.
  The user clicks a button and it continuously pings all IPS for 20 seconds..
  What I want to do is click a button to stop this.</p>
  <p>I created a function i called &quot;Process&quot; and it calls this process when the button is clicked.</p>
  <p>I tried to create a bool value and while loop for when it's false and made the stop button change the bool value to true, but it doesn't seem to stop anything, in fact I can't even close the window until the ping is completed.</p>
  <p>My code is below.</p>
  <pre><code>private bool _stopLoop;
  
      private void btnPlay_Click(object sender, EventArgs e)
      {
          Process();
      }
  
  private void Process()
  {
      while (_stopLoop == false)
      {
          for (int i = 0; i &lt; 20; i++)
          {
              foreach (DataGridViewRow dataGridView1Row in dataGridView1.Rows)
              {
                  var count = 20;
                  progressBar1.Value = i * progressBar1.Maximum / count;
                  Application.DoEvents();
                  var url = dataGridView1Row.Cells[1].Value.ToString();
                  int timeout = 500;
                  Ping ping = new Ping();
  
                  PingReply pingreply = ping.Send(url, timeout);
                  PingReply result = null;
                  IPStatus status;
  
                  result = pingreply;
                  status = result.Status;
  
                  if (status != IPStatus.Success)
                  {
                      dataGridView1Row.Cells[0].Style.BackColor = Color.Red;
                      dataGridView1Row.Cells[0].Value = &quot;Offline&quot;;
                  }
                  else
                  {
                      dataGridView1Row.Cells[0].Style.BackColor = Color.Green;
                      dataGridView1Row.Cells[0].Value = &quot;Online&quot;;
                  }
  
  
  
              }
          }
      }
  }
  
  private void btnStop_Click(object sender, EventArgs e)
  {
    _stopLoop = true;
  }
  </code></pre>