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);
}
}
//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?
1 Answer 1
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 =>
{
//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.
Explore related questions
See similar questions with these tags.