# Monitoring progress in Parallel.ForEach every minute

I'm using 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);
}
}

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?

Instead of checking the time from each thread on each iteration, I would use a 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 =>
{

I have also renamed cnt to count, those two characters are not worth it to make your code less readable.