You'll need to make the enumerator separate from your loop, but process it in parallel. Yield return will return a value of the enumerable collection - this wouldn't make sense, because one object would be using the enumeration NOT in parallel, but trying to return parallelized...
The normal way you'd do this is like:
// In your source, you use yield
public ClassImplementingIEnumerable: IEnumerable<int>
{
public IEnumerable<int> GetSource()
{
for (int i=0;i<1000;++i)
yield return i;
}
}
public class ParallelProcessingConsumer {
public void SomeMethod(ClassImplementingIEnumerable sourceProvider)
{
Parallel.ForEach(sourceProvider.GetSource(), parallelOptions, (i, loopState) =>
{
// Do work in parallel!
});
}
Reed Copsey, Jr. -
http://reedcopsey.com