Since Parallel.ForEach is part of the task library, Task.CurrentId will bring you closer to what you are looking for:
var data = new[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; Parallel.ForEach(data, new ParallelOptions { MaxDegreeOfParallelism = 4 }, i => { Console.WriteLine(Task.CurrentId); });
- 1 1 1 1 1 1 1 1 1 1 2 2 1
However, there is a refusal in the documents:
Task identifiers are assigned on demand and do not necessarily represent the procedure for creating task instances. Please note that although collisions are very rare, task identifiers are not guaranteed to be unique.
source share