0

I have a list of urls, and I want to download the source code using the WebClient class. Let's say that I have this urls:

private static string[] urls = 
{
   "google.com",
   "yahoo.com",
   "msn.com",
   "bing.com",
   "omarion.com",
   "bowwow.com"
};

It's a list of 6 urls. I want to use just 2 threads (at the same time) to download the source code.

I've seen that an option would be this:

private void RunAllActions(IEnumerable<Action> actions, int maxConcurrency)
{
    using(SemaphoreSlim concurrencySemaphore = new SemaphoreSlim(maxConcurrency))
    {
        foreach(Action action in actions)
        {
            Task.Factory.StartNew(() =>
            {
                concurrencySemaphore.Wait();
                try
                {
                    action();
                }
                finally
                {
                    concurrencySemaphore.Release();
                }
            });
        }
    }
}

but I don't know how to properly use it.

The idea is that I have an X number of links, but I want just Y threads working at the same time.

I've tried to do it with ThreadPool, but if I specify 5 threads, that number is not really taken into consideration. With this I mean, that even if I specify to run 5 threads at a time, 8 thread might start, or just 3 threads, depending on ... the situation I guess.

Yuval Itzchakov
  • 146,575
  • 32
  • 257
  • 321
user1812076
  • 269
  • 5
  • 21

0 Answers0