2009-10-21 58 views
4

我正在嘗試通過請求許多文件下載來測試另一個應用程序。爲什麼我不能有超過5個WebClient.DownloadFileAsync運行?

所以,我開始10 WebClient實例與以下代碼,但似乎我可以只有5在同一時間運行。

class Program 
{ 
    public static object locker = new object(); 
    public static void Main(string[] args) 
    { 
     for (int i = 0; i < 10; i++) 
      start(i); 
     Console.ReadLine(); 
    } 

    private static void start(object row) 
    { 
     DateTime start = DateTime.Now; 
     WebClient client = new WebClient(); 
     client.Credentials = CredentialCache.DefaultNetworkCredentials; 
     client.DownloadProgressChanged += (sender, e) => { 
      lock (locker){ 
       double throughput = e.BytesReceived/
        (DateTime.Now - start).TotalSeconds/1024/1024; 
       double error = 1 - (1/throughput); 
       Console.SetCursorPosition(0, (int)row); 
       Console.WriteLine(
        @"({0}) {1:HH\:mm\:ss.ffff} - {2:0.00}Mb - " + 
        @"{3:##0}% - {4:0.00}Mb/s ({5:+0.00%;-0.00%;0.00%}){6}", 
        row, DateTime.Now, e.BytesReceived/1024/1024, 
        e.ProgressPercentage, throughput, error, "  "); 
      } 
     }; 
     client.DownloadFileAsync(
      new Uri("http://site/Download.ashx?Id=123"), 
      String.Format("c:\\foo_{0}.xxx", row)); 
    } 
} 

我得到了以下的輸出:

 
(0) 14:51:07.1830 - 39,00Mb - 5% - 0,94Mb/s (-6,45%) 
(1) 14:51:06.8610 - 39,00Mb - 5% - 1,00Mb/s (+0,24%) 
(2) 14:51:06.5650 - 39,00Mb - 5% - 0,99Mb/s (-1,34%) 
(3) 14:51:07.2810 - 38,00Mb - 5% - 0,95Mb/s (-5,12%) 
(4) 14:51:06.5740 - 37,00Mb - 5% - 0,95Mb/s (-5,19%) 
(5) 14:50:30.4640 - 0,00Mb - 100% - 0,01Mb/s (-12690,64%) 
(6) 14:50:30.5390 - 0,00Mb - 100% - 0,01Mb/s (-12845,38%) 
(7) 14:50:30.8380 - 0,00Mb - 100% - 0,01Mb/s (-13909,70%) 
(8) 14:50:30.6150 - 0,00Mb - 100% - 0,01Mb/s (-12988,80%) 
(9) 14:50:30.9210 - 0,00Mb - 100% - 0,01Mb/s (-14079,53%) 

我可以改變這種狀況限制,模擬更多的併發用戶?

回答

1

您是否從同一臺機器啓動全部10個?檢查你的事件日誌。當你添加其他可能存在連接的東西時,你可能會遇到XP和以上的TCP/IP連接限制。

2

原因之一可能是,您的服務器(ASP.NET擴展名爲.ashx),只從進程下載文件請求並行。

你可以在你的web.config文件中改變它。

+0

+1也沒什麼看頭(例如,在Windows版本),雖然我忘記了改變它的財產。 – 2010-01-14 14:46:31

2

你需要增加最大連接到服務器在配置文件中,類似的併發線程限制存在WebClient的方法本身在某些系統上

<system.net> 

<connectionManagement> 

    <add address=「*「 maxconnection=「100「 /> 

</connectionManagement> 

</system.net> 
相關問題