True Parallel Downloads

前端 未结 2 1529
甜味超标
甜味超标 2020-12-21 19:00

This method i am using for concurrent downloads.

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


        
相关标签:
2条回答
  • 2020-12-21 19:41

    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;            
            }
    0 讨论(0)
  • 2020-12-21 19:44

    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);
        }
    }
    
    0 讨论(0)
提交回复
热议问题