Answer the question
In order to leave comments, you need to log in
How to keep a counter of successful operations using Parallel.ForEach?
There is a list of file paths. It is necessary to perform a certain action on each file and increase the counter if this action was completed successfully.
To speed up the processing of the list of files, it was decided to use a parallel loop. Processing the entire list began to take quite a bit of time, but the counter of successful operations began to give strange values.
For example, 250 files have been successfully processed, and the counter shows 510. Blocking the increment of a variable with the lock statement does not help, and it is extremely necessary to get the exact number of successful operations. Tell me, please, how can I accurately calculate the number of operations performed using the Parallel.ForEach parallel loop?
Below is the prototype code for the required task:
IEnumerable<string> files = new IEnumerable<string>();
files = Directory.EnumerateFiles(@"C:\test"); // +1300 путей к файлам
int successCounter = 0;
object _lock = new object();
Parallel.ForEach(files, f =>
{
if(Operation(f))
{
lock(_lock)
successCounter++;
}
});
Console.WriteLine($"Обработано: {successCounter.ToString()} из {files.Count().ToString()} файлов.");
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question