True Parallel Downloads

2019-01-29 02:19发布

问题:

This method i am using for concurrent downloads.

public void DownloadConcurrent(Action Method)
        {
            Action[] methodList = new Action[Concurent_Downloads];

            for (int i = 0; i < Concurent_Downloads; i++)
            {
                methodList[i] = Method;
            }

            Parallel.Invoke(methodList);
        }

I am trying to download urls simultaneously, but the No. of active downloads is always one.

like all the downloads will invoke, but only one url will start downloading data, not like all will start progressing the downloads.

I want all downloads to work parallel same time, unable to achieve that.

Update: the methord is using a Queue, it is downloading diffrent urls, form the queue.

回答1:

Instance members of a WebClient are not thread safe, so ensure that you have a separate instances in each action. In the method you have shown you seem to be multiplicating the same action delegate multiple times. So you are not downloading different urls, you are downloading the same url multiple times. And because the WebClient is not thread safe you might run into problems.

Here's an example of parallel downloads of multiple urls using the TPL:

using System;
using System.Linq;
using System.Net;
using System.Threading.Tasks;

class Program
{
    static void Main()
    {
        var urls = new[] 
        { 
            "http://google.com", 
            "http://yahoo.com", 
            "http://stackoverflow.com" 
        };

        var tasks = urls
            .Select(url => Task.Factory.StartNew(
                state => 
                {
                    using (var client = new WebClient())
                    {
                        var u = (string)state;
                        Console.WriteLine("starting to download {0}", u);
                        string result = client.DownloadString(u);
                        Console.WriteLine("finished downloading {0}", u);
                    }
                }, url)
            )
            .ToArray();

        Task.WaitAll(tasks);
    }
}


回答2:

To improve on @Darin-Dimitrov's answer, you can retrieve the result from the client.DownloadString() to use in another place using this improved function:


public List<string> DownloadUrlsInParallel(Uri[] urls)
        {            
            var tasks = urls
                .Select(url => Task.Factory.StartNew(
                    state =>
                    {
                        using (var client = new System.Net.WebClient())
                        {
                            var u = (Uri)state;
                            Console.WriteLine("starting to download {0}", u);
                            string result = client.DownloadString(u);
                            Console.WriteLine("finished downloading {0}", u);
                            return result;
                        }
                    }, url)
                )
                .ToArray();

            Task.WaitAll(tasks);
            List<string> ret = new List();
            foreach(var t in tasks)
            {
                ret.Add(t.Result);
            }
            return ret;            
        }