Parallel.ForEach how to know at which item ? - which loop?
c#, parallel.foreach
Solution
You can use the following overload of `Parallel.ForEach`:
public static ParallelLoopResult ForEach<TSource>(
IEnumerable<TSource> source,
Action<TSource, ParallelLoopState, long> body
)
It will pass the current element index to your delegate in the third parameter.
Problem
Example ``` var options = new ParallelOptions() { MaxDegreeOfParallelism = 10 }; List<string> lstAllUrls = File.ReadAllLines("myList.txt").ToList<string>(); Parallel.ForEach(lstAllUrls, options, myFunctionThatFetchPage); ``` Now this works perfectly fine. What I want to know is: How can I tell at which iteration I currently am. I mean lets say my list has 100000 URLs. It starts fetching them. But I also want to print on the screen which URL is getting fetched at the moment. How can I do that? How can I tell at which item of the given list the loop currently is?