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
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");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With