December 2019
Intermediate to advanced
346 pages
9h 8m
English
This is a variation of the ForEach loop wherein iterations may run in parallel. The source collection is partitioned and then the work is scheduled to run multiple threads. Parallel.ForEach works on generic collections and, just like the for loop, returns ParallelLoopResult.
The basic syntax of the Parallel.ForEach loop is as follows:
Parallel.ForEach<TSource>( IEnumerable<TSource> Source, Action<TSource> body)
An example of this is as follows. We have a list of ports that we need to monitor. We also need to update their statuses:
List<string> urls = new List<string>() {"www.google.com" , "www.yahoo.com","www.bing.com" };Parallel.ForEach(urls, url =>{ Ping pinger = new Ping(); Console.WriteLine($"Ping Url ...Read now
Unlock full access