Best way: implement interrupt / cancel function for all your threaded workers

So my question is how to implement the undo / interrupt function in all (I mean ALL) thread workers in your application in the best and most elegant way?

It doesn’t matter if it is an HttpWebRequest, an I / O operation or a calculation. The user should be able to cancel each action / thread at any time.

+3
source share
3 answers

Use .NET 4.0 Taskwith CancellationToken- this is a new universal cancellation system.

+6
source

/ .

Threading - , ... , , . , ! , , , , :

  • Thread 1 .
  • 2 , , .
  • 1 , , .
  • DEADLOCK: Thread 2 , .

, , , , , .. , , , , , ... , , . : .

, , . , , , .

, HttpWebRequest, / .

, , ! , , .

P.S.
, , Thread.IsBackground true, , .

+5

, , . - static volatile bool , , .

, , , . , , .

Thread.Abort() : .

0

Source: https://habr.com/ru/post/1746247/


All Articles