Pause and Resume a For-Loop in C#
<p>hey guys, m working on a windows App in C#, i have a for-loop which does some updation in a loop, and i have 3 buttons on the form named "Stop,Pause,Resume". So the purpose is same as the buttons named.. any1 outthere know this how to do?</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 wat i was thinking but it wont work
            {                   // it will go in end-less loop
              do                // how to stop this loop on "Resume" button click'd
              {

              }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 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>
