patterncsharpModerate
Monitoring progress in Parallel.ForEach every minute
Viewed 0 times
everymonitoringprogressparallelforeachminute
Problem
I'm using
It looks fine now but the lock statement part seems a bit ugly to me. Is there a better way to achieve this?
Parallel.ForEach to download 500K URLs and I want to monitor the number of URLs that have been successfully downloaded each minute.int elapsedMinutes = 0, cnt = 0;
Parallel.ForEach(list, tuple =>
{
var currMinutes = (int) ((DateTime.Now - startTime).TotalMinutes);
lock (Object)
{
if (currMinutes > elapsedMinutes)
{
elapsedMinutes = currMinutes;
Console.WriteLine("{0}: Extracted {1} urls", DateTime.Now, cnt);
}
}
//download urls ...
Interlocked.Increment(ref cnt);
});It looks fine now but the lock statement part seems a bit ugly to me. Is there a better way to achieve this?
Solution
Instead of checking the time from each thread on each iteration, I would use a
I have also renamed
Timer:int count = 0;
TimeSpan reportPeriod = TimeSpan.FromMinutes(0.1);
using (new Timer(
_ => Console.WriteLine("{0}: Extracted {1} urls", DateTime.Now, count),
null, reportPeriod, reportPeriod))
{
Parallel.ForEach(
list, tuple =>
{
//download urls ...
Interlocked.Increment(ref count);
});
}
Console.WriteLine("{0}: Done", DateTime.Now);I have also renamed
cnt to count, those two characters are not worth it to make your code less readable.Code Snippets
int count = 0;
TimeSpan reportPeriod = TimeSpan.FromMinutes(0.1);
using (new Timer(
_ => Console.WriteLine("{0}: Extracted {1} urls", DateTime.Now, count),
null, reportPeriod, reportPeriod))
{
Parallel.ForEach(
list, tuple =>
{
//download urls ...
Interlocked.Increment(ref count);
});
}
Console.WriteLine("{0}: Done", DateTime.Now);Context
StackExchange Code Review Q#65280, answer score: 10
Revisions (0)
No revisions yet.