Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 真正的并行下载_C#_.net_Windows_Download_Webclient - Fatal编程技术网

C# 真正的并行下载

C# 真正的并行下载,c#,.net,windows,download,webclient,C#,.net,Windows,Download,Webclient,我正在使用此方法进行并发下载 public void DownloadConcurrent(Action Method) { Action[] methodList = new Action[Concurent_Downloads]; for (int i = 0; i < Concurent_Downloads; i++) { methodList[i] = Meth

我正在使用此方法进行并发下载

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);
        }
public void DownloadConcurrent(操作方法)
{
Action[]methodList=新操作[Concurent_下载];
for(int i=0;i
我试图同时下载URL,但活动下载的数量始终为一

就像所有的下载都会调用一样,但是只有一个url会开始下载数据,而不是所有的url都会开始进行下载

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);
        }
我希望所有的下载同时并行工作,但无法实现这一点

更新:该方法使用队列,它下载不同的URL,形成队列。

的实例成员不是线程安全的,因此请确保在每个操作中都有单独的实例。在您展示的方法中,您似乎将同一个动作委托乘法多次。因此,您不是下载不同的url,而是多次下载同一url。而且由于WebClient不是线程安全的,您可能会遇到问题

以下是使用TPL并行下载多个URL的示例:

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);
    }
}
的实例成员不是线程安全的,因此请确保在每个操作中都有单独的实例。在您展示的方法中,您似乎将同一个动作委托乘法多次。因此,您不是下载不同的url,而是多次下载同一url。而且由于WebClient不是线程安全的,您可能会遇到问题

以下是使用TPL并行下载多个URL的示例:

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);
    }
}

要改进@Darin Dimitrov的答案,您可以从
客户端检索
结果
。DownloadString()
以在其他地方使用此改进的函数:


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;            
        }

公共列表下载URLSINPARALLEL(Uri[]URL)
{            
var tasks=url
.Select(url=>Task.Factory.StartNew(
状态=>
{
使用(var client=new System.Net.WebClient())
{
var u=(Uri)状态;
WriteLine(“开始下载{0}”,u);
字符串结果=client.DownloadString(u);
WriteLine(“已完成下载{0}”,u);
返回结果;
}
},网址)
)
.ToArray();
Task.WaitAll(任务);
List ret=新列表();
foreach(任务中的var t)
{
ret.Add(t.Result);
}
返回ret;

}
要改进@Darin Dimitrov的答案,您可以从
客户端检索
结果
。DownloadString()
以在其他地方使用此改进的函数:


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;            
        }

公共列表下载URLSINPARALLEL(Uri[]URL)
{            
var tasks=url
.Select(url=>Task.Factory.StartNew(
状态=>
{
使用(var client=new System.Net.WebClient())
{
var u=(Uri)状态;
WriteLine(“开始下载{0}”,u);
字符串结果=client.DownloadString(u);
WriteLine(“已完成下载{0}”,u);
返回结果;
}
},网址)
)
.ToArray();
Task.WaitAll(任务);
List ret=新列表();
foreach(任务中的var t)
{
ret.Add(t.Result);
}
返回ret;

}
动作委托内的代码是什么样子的?动作委托内的代码是什么样子的?