There are two ways you could do it.
The simplest way would be to have a variable that the long code would check:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
bool calculationHasBeenHalted = false;
void OnLongCodeButtonClick ()
{
// ...
if (calculationHasBeenHalted) return;
// ...
if (calculationHasBeenHalted) return;
// etc.
}
void OnHaltButtonClick ()
{
calculationHasBeenHalted = true;
}
|
Implementation of this may vary depending on your calculation.
An alternative, which may be more desirable since it will immediately halt processing, would be to use multithreading. I won't try to explain it since there are plenty of articles on the net that do a better job than I could, but the gist of it would be that you would launch a thread that does the processing by using
LaunchThread(...)
(assuming you're using Windows; if you're not, the process will still generally be the same). When the user presses the Stop button, you would call
TerminateThread(...)
to immediately stop the processing. Note that this may leave various data structures in an invalid state, depending on what sort of processing your thread is doing.
The first method is less accurate, but is safer and easier to implement. The second will cause the processing to stop immediately, but is more difficult to implement in a bug-free way.