parallel.for question
I have a foreach loop that processes a few thousand xmlnodes from an xmlnodelist. I'm trying to integrate the Parallel.For options from .net 4 but I get an error that "No overload for method 'For' takes 3 arguments'. But every example I see so far 开发者_StackOverflow社区has it written this way. Does someone know what I need to change to get this to compile? Thanks.
Parallel.For( 0, jobs.Count, i => { //..do work here.. });
Now the only other question I have is how do I break out of the current iteration? It doesn't seem to like my continue statements.
How exactly do you expect continue
statements to work when each iteration can be running concurrently? You should read How to: Stop or Break from a Parallel.For Loop on the MSDN for more information about this and take a look at the Stop and Break methods.
Now the only other question I have is how do I break out of the current iteration? It doesn't seem to like my continue statements.
Instead of continue
, use break
. Rather than:
for (int i = 1; i < jobs.Count; i++) {
// Initialise iteration.
if (iterationInvalid) continue;
// Do work.
}
What you're essentially getting is:
for (int i = 1; i < jobs.Count; i++)
action(i);
void action (int i) {
// Initialise iteration.
if (iterationInvalid) break;
// Do work.
}
精彩评论