Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to have multiple threads processing the same IEnumerable result?

I have a method which returns an IEnumerable<string> which of course is being handled with yield return <string>;. I want to have multiple threads processing the result of this, of course without repeating it and being thread safe. How would I achieve this?

var result = GetFiles(source);

for (int i = 0; i < Environment.ProcessorCount; i++)
{
    tasks.Add(Task.Factory.StartNew(() => { ProcessCopy(result); }));
}

Task.WaitAll(tasks.ToArray());

However this seems to be producing repeats:

C:\Users\esac\Pictures\2000-06\DSC_1834.JPG
C:\Users\esac\Pictures\2000-06\DSC_1835.JPG
C:\Users\esac\Pictures\2000-06\.picasa.ini
C:\Users\esac\Pictures\2000-06\DSC_1834.JPG
C:\Users\esac\Pictures\2000-06\DSC_1835.JPG
C:\Users\esac\Pictures\2000-06\.picasa.ini
C:\Users\esac\Pictures\2000-06\DSC_1834.JPG
C:\Users\esac\Pictures\2000-06\DSC_1835.JPG
C:\Users\esac\Pictures\2000-06\.picasa.ini
C:\Users\esac\Pictures\2000-06\DSC_1834.JPG
C:\Users\esac\Pictures\2000-06\DSC_1835.JPG
like image 629
esac Avatar asked Feb 23 '11 04:02

esac


1 Answers

You can easily do this using the Parallel.ForEach method.

Write a Simple Parallel.ForEach loop

Each iteration will be queued in the task manager. The loop will exit when all iterations are performed.

var result = GetFiles(source);

Parallel.ForEach(result, current => {
    ProcessCopy(current);
});

Console.WriteLine("Done");
like image 127
Josh Avatar answered Oct 18 '22 08:10

Josh