title: How to stop multiple threads (Tasks) from an external REST API call?
body: <p>So what I am dealing with here is a pretty complex server application that performs quite a long, complex and lengthy operation involving a number of threads and a number of tasks (yes, some are created as Tasks and some are &quot;manually&quot; created threads.</p>
  <p>The complex and lengthy process we are talking here is triggered using a REST API call (assume a web page generates the request object based on user input), and provides the user with an ID to poll for the progress of the operation.</p>
  <p>I am looking for an efficient way to allow the user to click an &quot;Abort/Stop&quot; button that will stop this whole orchestra of threads/tasks.</p>
  <p>(keep in mind this server can process a number of requests as described above).</p>
  <p>I have looked into a number of options, all of which seem to require the threads/tasks themselves to monitor for an &quot;abort flag&quot; during their operation loop and break out of the loop should it be required.</p>
  <p>Obviously using <code>Thread.Abort</code> is a big no no.</p>
  <p>I have thought about having some kind of an abstract class (say: <code>AbortableThread</code>) all which all my &quot;worker&quot; threads will have to implement, where the only abstract function would be Abort(), so that each thread can end in a clean manner, closing and finishing whatever it needs to.</p>
  <p>This way, I would perhaps be able to keep tabs on the threads that have been spawned by a specific user request and just call <code>Abort()</code> in a <code>foreach</code> loop.</p>
  <p>Despite that, I still try to figure out how would it be able to break into the loops I run, so my 2nd though would be that this &quot;abstract&quot; class could have a property <code>shouldBreak</code> which will again I will be able to set from &quot;outside&quot;, but this then brings me back to (almost) square one where I have to add &quot;logic&quot; into my threads to be able to abort.</p>
  <p>A 3rd idea I came up with is to have my logical loops call <code>Abort()</code> on every loop, without any validation whether an abort is currently required or not, where I will check my abstract's base class shouldBreak bool and act accordingly should it be required.</p>
  <p>There are no code examples and I have decided I would rather figure this out in high level before I dive into implementation.</p>
  <p>Thank you for reading this long question!</p>