abenci
abenci

Reputation: 8681

Progress update in Parallel.For() loops

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.

Upvotes: 5

Views: 2389

Answers (2)

Marc Gravell
Marc Gravell

Reputation: 1063338

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)

Upvotes: 7

Andrey
Andrey

Reputation: 60075

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

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

Upvotes: 3

Related Questions