多线程下载

时间:2014-11-15 16:31:07

标签: c# .net multithreading download system.net

我已经编写了下面的测试代码。它有效,但不像我预期的那样。由于某种原因,它仅适用于七个中的两个线程,另一个线程等待工作线程完成其任务。为什么会发生这种情况以及如何解决?请帮忙!

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

1 个答案:

答案 0 :(得分:0)

您应该设置 DefaultConnectionLimit

System.Net.ServicePointManager.DefaultConnectionLimit = 4;

默认情况下为2。