I have a backgroundworker with a few different functions and loops. Every example I found on the internet is only with one loop respectively one function. Now I want to know if there exists a short solution to cancel the worker with a button. I only know two solutions which are not optimal I think.
1: Add an if/else to every function/loop -> long and confusing code
loop
{
if (worker.CancellationPending == true)
{
e.Cancel = true;
break;
}
else
{
do calculation
}
}
2: Kill the backgroundworker thread with abort like here (How to “kill” background worker completely?)
I want that the calculation stops immediately after hitting the cancel button no matter which loop or function are executed at the moment. Are there any better oppotunities than mine?
Edit: And I don't know why How can I cancel a Task without LOOP should solve my problem because I have many different loops.