真正的并行下载 [英] True Parallel Downloads

查看:131
本文介绍了真正的并行下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个方法我使用的并发下载。

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

我试图同时下载的网址,但主动下载的号始终为1。

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.

推荐答案

的的 Web客户端不是线程安全的,所以保证你在每一个动作一个单独的实例。在你已经证明了你的方法似乎是multiplicating相同的动作代表多次。所以,你不能下载不同的网址,您正在下载相同的URL多​​次。而且由于Web客户端不是线程安全的,你可能会遇到问题。

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.

下面是并行下载多个URL中使用第三方物流的一个例子:

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

这篇关于真正的并行下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆