title: Pause and Resume a For-Loop in C#
body: <p>I'm working on a windows App in C#, I have a for-loop which update something in a loop, and I have 3 buttons on the form named "Stop,Pause,Resume". So the purpose is as same as the buttons named. Does anyone know how to do this?</p>
  
  <p>Here is the Loop</p>
  
  <pre><code>    private void btnCompleteAuto_Click(object sender, EventArgs e)
      {
          setGeneralValue();
          for (int i = 1; i &lt;= autoGridView.Rows.Count - 1; i++)
          {
              if (SRP == "Pause") // this is what I was thinking but it won't work
              {                   // it will step into end-less loop
                do                // how to stop this loop on "Resume" button click
                {
  
                }while(SRP!="Resume")
  
              }
              car = false;
              try
              {
                  MemberID = Convert.ToInt64(autoGridView.Rows[0].Cells["Member_ID"].Value);
                  DispID = Convert.ToString(autoGridView.Rows[0].Cells["Disp_Id"].Value);
                  Mobile = Convert.ToString(autoGridView.Rows[0].Cells["Mobile"].Value);
                  DueDate = Convert.ToString(autoGridView.Rows[0].Cells["Due_Date"].Value);
  
              }
              catch (Exception)
              {
                  MessageBox.Show("Row Not Found");
              }
  
              AutoRecharge(network_name, pack_name, Mobile, Mobile, Convert.ToString(autoGridView.Rows[0].Cells["Rck_Amt"].Value), vendor_id, vendor_pwd, pack_id, oxinetwork_id);
              autoGridView.Rows.RemoveAt(0);
          }
      }
  </code></pre>
  
  <p>Here are the 3 button events in which I'm setting a variable</p>
  
  <pre><code>    private void btnPause_Click(object sender, EventArgs e)
      {
          SRP = "Pause";
      }
  
      private void btnStop_Click(object sender, EventArgs e)
      {
          SRP = "Stop";
          autoGridView.DataSource = "";
      }
  
      private void btnResume_Click(object sender, EventArgs e)
      {
          SRP = "Resume";
      }
  </code></pre>