Break to stop the loop in parallel foreach

The below sample code iterates over a string list and uses the Break call to stop the loop at an arbitrary location.

Parallel.ForEach(urls, (url, loopState) => 
{
  if (url.Contains("w3mentor"))
  {
    loopState.Break();
  }
  results[url] = client.DownloadString(url);
});

Enjoyed this post? Share it!