there is a call call code by pressing the button

StartUp start = new StartUp(this); my = new Thread(start.Go); my.Start(); 

There is a code to end the stream by pressing another button.

 my.Abort(); 

and the function itself is passed to the stream

 public void Go () { int max = work.max_thread; label1: if (work.keys.Count>0) { while (count_thread == 0 || count_thread < max) { Thread myThread = new Thread(web.InD); myThread.Start(); count_thread += 1; } Thread.Sleep(1000); goto label1; } } 
  • 2
    use CancellationToken instead of Abort - 4per
  • four
    Abort - we lose it! goto - we lost him ... - Alexander Petrov
  • Why Abort does not help is not completely understood. goto - I like Pts - and than he is worse than the same while, which could be replaced here - it is not clear the same way - Arkhipov Vladimir
  • one
    @ArhipovVladimir GoTo is too powerful a design that can be used for anything ... For branching, loops, and so on. However, you must sufficiently justify why you are using GoTo, and not another operator. After all, looking at GoTo you will not immediately understand its meaning, but when you see some while you will immediately understand the purpose and meaning of the code. - iluxa1810
  • one
    How to finish threads correctly: en.stackoverflow.com/a/573594/106 - andreycha

1 answer 1

Abort does not guarantee a kill stream. Then it can catch an exception and execute Thread.ResetAbort and continue execution.

The thread interruption must be a cooperative operation, so you need to use CancellationToken .

PS GoTo - this is a bad tone and its use must be thoroughly justified.

You can use it in rare cases when you need to jump out of a large number of conditions without performing a large number of status checks.

  • one
    But the author in the code does not have exception handling. The code in general has no reason not to end with Abort. Most likely an error, as usual, in the code that the author did not show. - MSDN.WhiteKnight
  • yes, just in the streams that the interrupted thread causes - a lot of code in try processing - Arkhipov Vladimir