Timeout for Action in Parallel.ForEach iteration

前端 未结 3 2227
[愿得一人]
[愿得一人] 2021-02-14 22:10

I have something similar to this in my code:

Parallel.ForEach(myList, new ParallelOptions { MaxDegreeOfParallelism = 4 }, item =>
{
    Process(item);
});
         


        
3条回答
  •  悲&欢浪女
    2021-02-14 22:58

    Consider adding CancellationToken to your code. This way, at any point you can properly cancel all the operations.

    Then, you can use the CancelAfter() method.

提交回复
热议问题