Foreach' failing when using Parallel Task Library?

The reason why is down to the way C# evaluates anonymous methods, they're not true closures. It really has nothing to do with the TPL. The following code prints out all d's.

This is not what yoy would expect List tasks = new List(); List lists = new List(); lists. AddRange(new string { "a", "b", "c", "d" }); foreach (var list in lists) { tasks. Add(Task.Factory.

StartNew(() => { Console. WriteLine(list); })); } The reason is because the value of list when the anonymous method was created is not the one that gets evaluated in the method body. The value of list at the time the method was executed is used.

You can force a fix for this by doing the following: List tasks = new List(); List lists = new List(); lists. AddRange(new string { "a", "b", "c", "d" }); foreach (var list in lists) { var localList = list; tasks. Add(Task.Factory.

StartNew(() => { Console. WriteLine(localList); })); } You don't have to pass in the list value to the anonymous method explicitly This blog post goes into this in much more detail: http://blogs.msdn.com/b/abhinaba/archive/2005/10/18/482180.aspx.

The reason why is down to the way C# evaluates anonymous methods, they're not true closures. It really has nothing to do with the TPL. The following code prints out all d's.

This is not what yoy would expect List tasks = new List(); List lists = new List(); lists. AddRange(new string { "a", "b", "c", "d" }); foreach (var list in lists) { tasks. Add(Task.Factory.

StartNew(() => { Console. WriteLine(list); })); } The reason is because the value of list when the anonymous method was created is not the one that gets evaluated in the method body. The value of list at the time the method was executed is used.

You can force a fix for this by doing the following: List tasks = new List(); List lists = new List(); lists. AddRange(new string { "a", "b", "c", "d" }); foreach (var list in lists) { var localList = list; tasks. Add(Task.Factory.

StartNew(() => { Console. WriteLine(localList); })); } You don't have to pass in the list value to the anonymous method explicitly. This blog post goes into this in much more detail: http://blogs.msdn.com/b/abhinaba/archive/2005/10/18/482180.aspx.

Apologies for not replying to this earlier. I found a solution - although I don't understand why it works... Originally, I had this ... foreach (var list in lists) { tasks. Add(factory.

StartNew(() => { WriteListToLogFile(list); })); } Changing the sequential foreach to a parallel foreach fixes the problem... Parallel. ForEach(lists, list => tasks. Add(factory.

StartNew(() => { WriteListToLogFile(list); })); ).

You really shouldn't have to use Parallel. ForEach instead of foreach. This will be very inefficient for doing some thing as lightweight as starting tasks.

See my answer for an explanation. – Ade Miller Jul 1 '10 at 7:09.

I am not sure why you have a list for "tasks", you are only ever using one of them. Edit: factory. StartNew Creates and starts a System.Threading.Tasks.

Task! Thinking out loud: so there is a separate task for each of the List in its list which calls WriteListToLogFile? I think you will need to use ThreadPool.

QueueUserWorkItem in your code after task. Add look at this example (see the accepted answer post) link.

1 You haven't read the code correctly. Factory. StartNew(...) creates and starts a new Task for each item in the lists collection.

– Chris Arnold Jun 2 '10 at 9:48 +1 ok I see my mistake now :) thanks! The var declaration got me confused. – VoodooChild Jun 2 '10 at 9:59 ThreadPool is the old way of doing parallel programming and doesn't give as much feedback and control as the new Parallel Task Library.

– Chris Arnold Jun 2 '10 at 11:23.

Ran into this same problem myself. I'm still not sure why it happens, but I was able to get it to work properly by passing in a state object foreach (var list in lists) { tasks. Add(factory.

StartNew((o) => { var l = o as List; WriteListToLogFile(l); }, list)); }.

You shouldn't have to use a state object. See my answer/explanation. – Ade Miller Jul 1 '10 at 7:11.

To speed things up I wanted do use the package doParallel. Here, the merge function seems to be the problem. Task 1 failed - "cannot coerce class 'c("xts", "zoo")' into a data.

However, when changing %dopar% to a normal serial %do% everything works as accepted. Till now I was not able to find any solution to this problem and I'm not even sure what to look for.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions