2014-11-15 4 views
0

아래 테스트 코드를 작성했습니다. 그것은 작동하지만, 예상했던대로는 아닙니다. 어떤 이유로 7에서 두 개의 스레드에 대해서만 작동하며, 다른 스레드는 작업 스레드가 작업을 마칠 때까지 기다립니다. 왜 이런 일이 일어나고 어떻게 고칠 수 있습니까? 도와주세요 !다중 스레드 다운로드

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

답변

0

당신은 DefaultConnectionLimit을 설정해야합니다. 기본적으로

System.Net.ServicePointManager.DefaultConnectionLimit = 4; 

는 2

+0

퍼센트입니다! @ # 감사합니다! 하지만이 설정을 보는 위치와 방법은 무엇입니까? 디버깅하는 동안? – L6go1as

관련 문제