Threads are possible, but not necessary.
Threading[
^] may be necessary because of other circumstances. But simply killing a thread to stop a background operation may result in issues described
here[
^].
The start button starts a method. The stop button cannot directly stop this method. But the method can be created with the possibility in mind that there may be a need to stop it.
To implement this, the method has to repeatedly check if it should continue. Looping or recoursive methods can easily be built that way.
private bool continue = true;
private void StopButton_Click(object sender, EventArgs e)
{
continue = false;
}
private void StartButton_Click(object sender, EventArgs e)
{
continue = true;
LoopThroughNumbers();
}
private void LoopThroughNumbers()
{
for(int i = 0; i < 10000; i++)
{
if(!continue)
{
break;
}
DoSomethingWith(i);
}
}
private void RecourseThroughNumbers(int i)
{
if(!continue)
{
return;
}
if(i >= 10000)
{
return;
}
DoSomethingWith(i);
RecourseThroughNumbers(i + 1);
}