简体   繁体   English

多线程下载

[英]Multi-Thread download

I have wrote test-code that below. 我在下面写了测试代码。 It works, but not as I expected to. 它可以工作,但不像我预期的那样。 For some reason it works only for two threads from seven, another threads wait for that working threads finished their tasks. 由于某些原因,它仅适用于七个线程中的两个,另一个线程等待该工作线程完成其任务。 Why this happens and how to fix it ? 为什么会发生这种情况以及如何解决? Please help ! 请帮忙 !

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;
using System.Net;
using System.IO;
using System.Collections.Concurrent;

namespace WindowsFormsApplication7
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        Queue<string> Links = new Queue<string>();
        Queue<string> Patch = new Queue<string>();

        private void button1_Click(object sender, EventArgs e)
        {
            string[] str = { "http://public.ag.ru/vd/00000000000000000000000000000000/patches/4672/HQ_Townmaps.exe", "http://public.ag.ru/vd/00000000000000000000000000000000/demos/5886/aa112502.exe", 
                               "http://public.ag.ru/vd/00000000000000000000000000000000/demos/21471/AvadonDemo.exe", "http://public.ag.ru/vd/00000000000000000000000000000000/demos/4972/aow2finaldemo_rc1.exe", 
                               "http://public.ag.ru/vd/00000000000000000000000000000000/demos/16294/AM_Grimm_Free_Episode_1_Xtreme_Repack.exe", "http://public.ag.ru/vd/00000000000000000000000000000000/demos/12467/at3_demo_ag.exe", 
                               "http://public.ag.ru/vd/00000000000000000000000000000000/demos/13482/aox_spdemo_install.exe" };

            string[] rts = { "HQ_Townmaps.exe", "aa112502.exe", "AvadonDemo.exe", 
                               "aow2finaldemo_rc1.exe", "AM_Grimm_Free_Episode_1_Xtreme_Repack.exe", "at3_demo_ag.exe", "aox_spdemo_install.exe" };

            for (int i = 0; i < str.Length; i++ )
            {
                Links.Enqueue(str[i]);
                Patch.Enqueue(rts[i]);
            }

            List<Thread> TList = new List<Thread>();

            for (int i = 0; i < 7; i++)
            {
                Thread t = new Thread(DoWork);
                t.IsBackground = true;
                TList.Add(t);
                TList[i].Start();
            }
        }

        private void DoWork()
        {
            try
            {
                Thread.Sleep(100);

                HttpWebRequest webRequest = WebRequest.Create(Links.Dequeue()) as HttpWebRequest;
                webRequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
                webRequest.Headers.Add("Accept-Language: ru-RU,ru;q=0.8,en-US;q=0.6,en;q=0.4");
                webRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.124 Safari/537.36";
                webRequest.Timeout = Timeout.Infinite;
                webRequest.KeepAlive = true;

                HttpWebResponse webResponse = webRequest.GetResponse() as HttpWebResponse;

                byte[] buffer = new byte[(int)webResponse.ContentLength];
                int bytesRead = 0;

                using (Stream stream = webResponse.GetResponseStream())
                {
                    using (FileStream fileStream = File.Create(Patch.Dequeue()))
                    {
                        while ((bytesRead = stream.Read(buffer, 0, buffer.Length)) != 0)
                        {
                            fileStream.Write(buffer, 0, bytesRead);
                        }
                    }
                }
            }
            catch (Exception e) { MessageBox.Show(e.Message); }
        }
    }
}

You should set DefaultConnectionLimit . 您应该设置DefaultConnectionLimit

System.Net.ServicePointManager.DefaultConnectionLimit = 4;

By default it is 2. 默认为2。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM