Multi-Thread download

1.4k views Asked by At

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

There are 1 answers

1
EZI On BEST ANSWER

You should set DefaultConnectionLimit.

System.Net.ServicePointManager.DefaultConnectionLimit = 4;

By default it is 2.