views:

20

answers:

2

As you can guess the index of the Parallel.For() loop jumps from one value to the other. How can I estimate the amount of the work done?

Thanks.

+1  A: 
int progress = 0;
Parallel.For( from, to, i => {
// do the job
Interlocked.Increment(ref progress);
});

now actual progress is (float)(to - from) / progress

Andrey
No way to get this value without interfering with thread synchronization?
devdept
@devdept no. but what is the problem using this method?
Andrey
@Andrey: I thought the the Parallel.For() included a more elegant way to get this value.
devdept
@devdept you have to add two lines of code, what is not elegant here?
Andrey
+1  A: 

By keeping a counter rather than looking at the index? For example:

int counter  = 0;
Parallel.For(4, 500, i => {
    // TODO: something useful...         
    int progress = Interlocked.Increment(ref counter);
    Console.WriteLine("{0}: {1}", progress, i);
});

(the Interlocked usage is essential to avoid getting race-conditions on accessing counter)

Marc Gravell