The direct answer to your question is: curry the outer loop state and break it as you wish:
Parallel.ForEach(listOfCities, ParallelCityOptions, (city, outerLoopState) =>
{
Parallel.ForEach(listOfPeople, ParallelPeopleOptions, (person, innerLoopState) =>
{
if (person == "Bill")
{
// do something
outerLoopState.Break();
innerLoopState.Break();
Console.WriteLine("Should never hit this line.");
}
});
});
However all of the elements in your array will be processed by your code at once, since there's so few compared to the number of cores a modern computer has. Simply requesting a parallel loop break won't magically break all threads, it will just stop new ones from being processed.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…