6
\$\begingroup\$

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?

Jamal
35.2k13 gold badges134 silver badges238 bronze badges
asked Oct 10, 2014 at 14:58
\$\endgroup\$

1 Answer 1

10
\$\begingroup\$

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.

answered Oct 10, 2014 at 16:35
\$\endgroup\$
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.