Questions tagged [parallel.foreach]

Parallel.ForEach is a .NET method for processing an enumerable sequence in parallel.

Parallel.ForEach is a .NET method for processing an enumerable sequence in parallel. It is part of the .

1001 questions
371
votes
5 answers

How can I limit Parallel.ForEach?

I have a Parallel.ForEach() async loop with which I download some webpages. My bandwidth is limited so I can download only x pages per time but Parallel.ForEach executes whole list of desired webpages. Is there a way to limit thread number or any…
eugeneK
  • 10,750
  • 19
  • 66
  • 101
316
votes
3 answers

Is there an equivalent to 'continue' in a Parallel.ForEach?

I am porting some code to Parallel.ForEach and got an error with a continue I have in the code. Is there something equivalent I can use in a Parallel.ForEach functionally equivalent to continue in a foreach loop? Parallel.ForEach(items,…
283
votes
10 answers

Parallel foreach with asynchronous lambda

I would like to handle a collection in parallel, but I'm having trouble implementing it and I'm therefore hoping for some help. The trouble arises if I want to call a method marked async in C#, within the lambda of the parallel loop. For…
clausndk
  • 3,129
  • 2
  • 18
  • 14
233
votes
4 answers

Parallel.ForEach vs Task.Run and Task.WhenAll

What are the differences between using Parallel.ForEach or Task.Run() to start a set of tasks asynchronously? Version 1: List strings = new List { "s1", "s2", "s3" }; Parallel.ForEach(strings, s => { DoSomething(s); }); Version…
Petter T
  • 3,387
  • 2
  • 19
  • 31
227
votes
11 answers

Nesting await in Parallel.ForEach

In a metro app, I need to execute a number of WCF calls. There are a significant number of calls to be made, so I need to do them in a parallel loop. The problem is that the parallel loop exits before the WCF calls are all complete. How would you…
Darthg8r
  • 12,377
  • 15
  • 63
  • 100
208
votes
6 answers

How can I convert this foreach code to Parallel.ForEach?

I am a bit of confused about Parallel.ForEach. What is Parallel.ForEach and what does it exactly do? Please don't reference any MSDN link. Here's a simple example : string[] lines = File.ReadAllLines(txtProxyListPath.Text); List
SilverLight
  • 19,668
  • 65
  • 192
  • 300
154
votes
3 answers

Parallel.ForEach() vs. foreach(IEnumerable.AsParallel())

Erg, I'm trying to find these two methods in the BCL using Reflector, but can't locate them. What's the difference between these two snippets? A: IEnumerable items = ... Parallel.ForEach(items, item => { ... }); B: IEnumerable
132
votes
5 answers

Break parallel.foreach?

How do I break out of an parallel.for loop? I have a pretty complex statement which looks like the following: Parallel.ForEach(ColorIndex.AsEnumerable(), new Action((ColorIndexHolder Element) => { …
Rasmus Søborg
  • 3,597
  • 4
  • 29
  • 46
120
votes
6 answers

Does Parallel.ForEach limit the number of active threads?

Given this code: var arrayStrings = new string[1000]; Parallel.ForEach(arrayStrings, someString => { DoSomething(someString); }); Will all 1000 threads spawn almost simultaneously?
Jader Dias
  • 88,211
  • 155
  • 421
  • 625
72
votes
6 answers

How do I collect return values from Parallel.ForEach?

I'm calling a slow webservice in parallel. Things were great until I realized I need to get some information back from the service. But I don't see where to get the values back. I can't write to the database, HttpContext.Current appears to be null…
MatthewMartin
  • 32,326
  • 33
  • 105
  • 164
66
votes
4 answers

What is the correct usage of ConcurrentBag?

I've already read previous questions here about ConcurrentBag but did not find an actual sample of implementation in multi-threading. ConcurrentBag is a thread-safe bag implementation, optimized for scenarios where the same thread will be both…
60
votes
6 answers

What does MaxDegreeOfParallelism do?

I am using Parallel.ForEach and I am doing some database updates, now without setting MaxDegreeOfParallelism, a dual core processor machine results in SQL client timeouts, where else quad core processor machine somehow does not timeout. Now I have…
59
votes
4 answers

Parallel.ForEach and async-await

I had such method: public async Task GetResult() { MyResult result = new MyResult(); foreach(var method in Methods) { string json = await Process(method); result.Prop1 = PopulateProp1(json); …
Sergino
  • 10,128
  • 30
  • 98
  • 159
50
votes
1 answer

Async/await and parallel in C#

When should I use async/await and when should I use parallel.foreach in C#? Are parallel and async/await serve the same purpose? What are the differences in them?
AUser123
  • 651
  • 1
  • 7
  • 21
39
votes
6 answers

Parallel.ForEach slower than foreach

Here is the code: using (var context = new AventureWorksDataContext()) { IEnumerable _customerQuery = from c in context.Customers where c.FirstName.StartsWith("A") …
Mike Diaz
  • 2,045
  • 4
  • 32
  • 55
1
2 3
66 67