使用任务和WebClient同时下载

时间:2015-11-26 11:42:10

标签: c# multithreading

我正在做一些完全错误或者只是不理解多线程/任务的概念。我只想说,5个任务同时从互联网上下载字符串。在控制台上它应该立即放置5个日志,但它们不是即时的。对不起我的可怕解释,但希望我的代码将清除。

class ThreadedDownloadWorker
{
    public Dictionary<string,string> urls { get; set; }
    public List<Task> tasks = new List<Task>();
    public string downloadGuid;
    public int progress = 0;
    public MainForm main;
    public int amountOfPages;

    public ThreadedDownloadWorker(MainForm main, int amountOfPages)
    {
        this.main = main;
        this.amountOfPages = amountOfPages;
    }

    public void startDownloadStrings(int maxAmountOfThreads, string downloadGuid)
    {
        this.downloadGuid = downloadGuid;
        for (int i = 0; i < maxAmountOfThreads; i++) 
        {
            if (urls.Count > 0) 
            {
                KeyValuePair<string, string> selectedUrl = urls.First();
                Task task = Task.Factory.StartNew(() => doDownloadStrings());
                tasks.Add(task);
            }
        }
    }

    public void doDownloadStrings() 
    {
        KeyValuePair<string, string> selectedUrl = urls.First();
        urls.Remove(selectedUrl.Key);
        WebClient wc = new WebClient();
        Console.WriteLine("Started download: " + selectedUrl.Key);
        string downloadedString = wc.DownloadString(selectedUrl.Value);
        SettingsAndData.saveDownloadPage(downloadGuid, selectedUrl.Key + ".json", downloadedString);
        progress++;
        this.main.blogs[this.main.selectedGuid].status = "Downloaded " + progress + " / " + this.amountOfPages.ToString();
        this.main.reloadBlogGridThreaded();
        wc.Dispose();
        if (urls.Count > 0) 
        {
            //doDownloadStrings();
        }
    }
}

这就是我调用此代码的方式:

var amountOfPages = 28;     
Classes.ThreadedDownloadWorker threadedWorker = new Classes.ThreadedDownloadWorker(this, amountOfPages);
threadedWorker.urls = new Dictionary<string, string>();
for(int i = 0; i < amountOfPages; i++)
{
    threadedWorker.urls.Add((i * 50).ToString(), "http://justarandompage.bla.bla/?offset="+ (i * 50).ToString());
}
threadedWorker.startDownloadStrings(5, selectedGuid);

根据我的理解,这应该立即启动5个下载任务。但是当我检查控制台时,它们不会立即启动,而是一个接一个地启动,每个之间可能有500毫秒的延迟。

1 个答案:

答案 0 :(得分:1)

尝试使用服务点管理器,并设置默认连接限制

https://msdn.microsoft.com/en-us/library/system.net.servicepointmanager%28v=vs.110%29.aspx

这将消除仅两个连接的限制