Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

7
  • and the ct in async (OrderNumber, ct) is the CancellationToken? Commented Dec 6, 2021 at 17:37
  • 2
    @DoctorFord yes. Commented Dec 6, 2021 at 17:39
  • 1
    There is an issue with your code - you can't actually use (ct) since you never provided it. You need to pass the cancellation token in like this Parallel.ForEachAsync(result, myCancelToken, async (OrderNumber, ct) => { }); Or, it appears you can also assign your token to the CancellationToken property of the ParallelOptions Commented Feb 22, 2022 at 20:06
  • 1
    @snow_FFFFFF "you can't actually use (ct) since you never provided it." - it depends on what do you mean/understand by "use". What I meant by use is to analyze it's canceled state - in this case you actually can cause Parallel.ForEachAsync will provide one if programmer has not (there are situations when ForEachAsync implementation will cancel it, one that I know of - one of the tasks will result in exception). "You need to pass the cancellation token" as I explained - no, it is not required. Commented Feb 23, 2022 at 7:46
  • 3
    Thanks for the sample - my first reaction was that nothing was actually using the token, but I do see that the exception does cause it to indicate cancellation. This certainly confirms that the MS documentation isn't clear to me, but hopefully this string of comments will be helpful to someone else :) Commented Feb 24, 2022 at 15:53