breaking out of my infinite loop
<p>I have two buttons one for start and one for stop in my UI form,and i have one infinite loop that executes some function in my class name programs in a method.The start button is clicked by the user it invokes this method to execute the infinite loop and i need to break this infinite loop when the user clicks the stop button,after that my compiler will break out of this infinite loop and enters to the code inside the button stop click. 
I am trying to use the <code>Application.DoEvents()</code> method,this is working well if my infinite loop code is inside of the start button click,but if my infinite loop code is in the new class which is created by me i.e programs,how can use the <code>Application.DoEvents()</code> method to break out of this infinite loop.</p>

<p>Example:</p>

<pre><code>namespace inFiniteLoopTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        bool stopBtnClk = false;
        bool startBtnClk = false;

        private void StartBtn_Click(object sender, EventArgs e)
        {
            stopBtnClk=false;

            startBtnClk = true;

            while(true)
            {
                //some code to be executed
                Application.DoEvents();
                if (stopBtnClk == true)
                {
                   break;
                }
            }
        }

        private void StopBtn_Click(object sender, EventArgs e)
        {
            stopBtnClk = true;
            if (startBtnClk == true)
            {
                //Application.Exit();
                MessageBox.Show("success");

            }

        }
</code></pre>

<p>this is working well.</p>

<p>But</p>

<pre><code>public class programs
{    
    public static void infiniteLoop(bool stopBtnClick)
    {    
        while(true)
        {
            //some code to be executed
            Application.DoEvents();
            if (stopBtnClk == true)
            {
                break;
            }
        }
    }    
} 

//and my UI code to call this class is
namespace inFiniteLoopTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        bool stopBtnClk = false;
        bool startBtnClk = false;

        private void StartBtn_Click(object sender, EventArgs e)
        {
            stopBtnClk=false;    
            startBtnClk = true;
            programs.infiniteLoop(stopBtnClk);
        }

        private void StopBtn_Click(object sender, EventArgs e)
        {
            stopBtnClk = true;
            if (startBtnClk == true)
            {
                //Application.Exit();
                MessageBox.Show("success");
            }
        }            
    }
</code></pre>

<p>but this is not working .</p>

<p>Even if the compiler displays the message "success" when the stop button is clicked, but the debugger still said running in my form.
I hope my question is clear.
And i am kindly requesting you to answer my question as soon as possible and get rid of this problem!
I openly accept your answer if you come especially with a thread.</p>

<p>sorry i am a beginner for C#, but i need to continue on that.</p>

<p>Thank you!!</p>
