Сделайте что-то вроде этого:
int current = 0;
object lockCurrent = new object();
Parallel.For(0, list.Count,
new ParallelOptions { MaxDegreeOfParallelism = MaxThreads },
(ii, loopState) => {
// So the way Parallel.For works is that it chunks the task list up with each thread getting a chunk to work on...
// e.g. [1-1,000], [1,001- 2,000], [2,001-3,000] etc...
// We have prioritized our job queue such that more important tasks come first. So we don't want the task list to be
// broken up, we want the task list to be run in roughly the same order we started with. So we ignore tha past in
// loop variable and just increment our own counter.
int thisCurrent = 0;
lock (lockCurrent) {
thisCurrent = current;
current++;
}
dothework(list[thisCurrent]);
});
Вы можете видеть, как при выходе из параллельного цикла for вы узнаете последний элемент списка, который должен быть выполнен, при условии, что все потоки заканчиваются до прерывания,Я не большой поклонник PLINQ или LINQ.Честно говоря, я не понимаю, как написание LINQ / PLINQ приводит к удобству сопровождения исходного кода или удобочитаемости ... Parallel.For - гораздо лучшее решение.