簡體   English   中英

為什么我不能運行超過5個WebClient.DownloadFileAsync?

[英]Why I can't have more than 5 WebClient.DownloadFileAsync running?

我正在嘗試通過請求許多文件下載來測試另一個應用程序。

所以,我用以下代碼啟動了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%)

我可以更改該限制以模擬更多並發用戶嗎?

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

您可以在web.config文件中更改此設置。

您需要在配置文件中增加與服務器的最大連接數

<system.net>

<connectionManagement>

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

</connectionManagement>

</system.net>

你是從同一台機器上發射全部10個嗎? 檢查您的事件日志。 當您添加可能存在連接的其他內容時,您可能會遇到強加於XP及以上的TCP / IP的10連接限制。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM