Here is a sample having the loop in a thread:
private void button1_Click(object sender, EventArgs e)
{
System.Threading.Thread thr = new Thread(ThreadProc);
thr.Start();
}
private void button2_Click(object sender, EventArgs e)
{
bCancel = true;
}
bool bCancel = false;
private void ThreadProc(object o)
{
while (!bCancel)
{
Thread.Sleep(1);
}
MessageBox.Show("Loop finished");
}