AFAIK, SQL does not support threading. But, the principle is pretty simple. This example uses a BackgroundWorker - which is probably all you will need for this kind of task:
BackgroundWorker bw;
private void button1_Click(object sender, EventArgs e)
{
Button b = sender as Button;
if (b != null && b.Text == "Start")
{
bw = new BackgroundWorker();
bw.WorkerSupportsCancellation = true;
bw.DoWork += new DoWorkEventHandler(bw_DoWork);
bw.RunWorkerAsync();
b.Text = "Stop";
}
else
{
if (bw.IsBusy)
{
bw.CancelAsync();
}
b.Text = "Start";
}
}
void bw_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;
int i = 0;
while (true)
{
if (worker.CancellationPending == true)
{
e.Cancel = true;
break;
}
else
{
Console.WriteLine(i++);
System.Threading.Thread.Sleep(500);
}
}
}