Developer, Former MVP, now at Microsoft - Best of 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013
IEnumerable<string> arr = ...foreach (string item in arr){ // Do something}
Parallel.ForEach<string>(arr, delegate (string item){ // Do something});
Parallel.ForEach (arr, (string item) =>{ // Do something});
Parallel
ForEach
IEnumerable
TSource
Action
Task
SelfReplicating
static string[] arr = Directory.GetFiles(@"C:\Users\Public\Pictures\Sample Pictures", "*.jpg"); static void SimulateProcessing() { Thread.SpinWait(100000000); } static string TID { get { return " TID = " + Thread.CurrentThread. ManagedThreadId.ToString(); } }
Main
foreach (string ip in arr) { Program.SimulateProcessing(); Console.WriteLine(ip + TID); } Console.ReadLine();
Parallel.ForEach(arr, (string ip) => { Program.SimulateProcessing(); Console.WriteLine(ip + TID);});
Parallel.ForEach
foreach
pfor
For
Invoke