I needed parallel processing of the queue, I implemented it, but there is a problem in that at a certain place of the handler I need to implement stopping the thread for a certain time.

Launch Parallel:

Parallel.ForEach(list, Start); 

Handler:

  private void Start(string param) { Class class = new class(param); class.Start(); } Class.Start public void Start() { DownloadAll(); //.... } 

And in the DownloadAll method, I need to implement stopping the stream for a certain time (set programmatically). Thread.Sleep , as I understand it, does not fit here. I ask for help, preferably with sample code.

  • 3
    It inhibits the current. but Parallel.ForEach is not so dumb as to create new streams and not use the original one. Therefore, he actively uses the initial flow and you slow it down. - vitidev
  • 2
    Thread.Sleep is required to stop the thread. It can not be that it does not work, and you noticed it first. Check again, possibly outside the debugger. - VladD
  • one
    This is a fun debugger feature like this. When you take a step, the debugger puts a breakpoint on the next instruction and resumes the program. But at this breakpoint may have another thread. That’s why it seems to you that Thread.Sleep does not work. - Pavel Mayorov
  • one
    And to avoid fun debugging, install ParallelOptions {MaxDegreeOfParallelism = 1} when debugging and there will be one stream without jumping, and then do not forget to remove it. - vitidev
  • 2
    I wrote above. It inhibits the current. But Parallel.Foreach uses And the stream that started (this is CPU-bound tools) and if there was a main UI stream, where Parallel.Foreach started, then the UI stream will be used inside as well. And with MaxDegreeOfParallelism = 1, there will be no need to pull threads from the pool at all. Wrap everything in Task.Run and then you untie the UI stream - vitidev

0