how to convert:
foreach ( NotifyCollectionChangedEventHandler handler in delegates) {
...
}
To somthing like This
Parallel.ForEach( NotifyCollectionChangedEventHandler handler in delegates) {
...
}
ForEach loop works like a Parallel. For loop. The loop partitions the source collection and schedules the work on multiple threads based on the system environment. The more processors on the system, the faster the parallel method runs.
The execution of Parallel. Foreach is faster than normal ForEach.
Why C#'s foreach loop cannot change what it loops over. With a foreach loop we easily iterate over all elements in a collection. During each pass through the loop, the loop variable is set to an element from a collection.
To use Parallel. ForEach loop we need to import System. Threading. Tasks namespace in using directive.
You can do:
Parallel.ForEach(delegates, handler =>
{
//your stuff
});
Consider the following example
List<string> list = new List<string>()
{
"ABC",
"DEF",
"EFG"
};
Parallel.ForEach(list, str =>
{
Console.WriteLine(str);
});
You may also see: How to: Write a Simple Parallel.ForEach Loop
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