I have a List<someObject>
and an extention method to someObject
called Process
.
Consider the following
private void processList(List<someObject> list)
{
Task.Factory.StartNew(() =>{
foreach(var instance in list)
{
instance.Process();
}});
}
but I wanted multiple Task
s to work on the same list, to avoid locking I cloned the list and divided it into 2 lists, then started 2 separate Task
s to process, the speed has almost doubled.
I was wondering if there are built in things in .NET that could help me achieve such things in a dynamic way and with cleaner syntax?
You can use TPL's Parallel.Foreach method
var list = new List<SomeClass>();
Parallel.ForEach(list, instance =>
{
instance.Process();
});
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