如何正确下载多个文件更快和异步?

时间:2015-01-19 12:58:15

标签: c# .net asynchronous httpwebrequest

我尝试使用

中的脚本

http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.begingetresponse%28v=vs.110%29.aspx

但是当我尝试下载多个文件时,根据请求增加了下载站点(同一站点)所消耗的时间,结果是:

retrive time: 1500 ms
retrive time: 1500 ms
retrive time: 2721 ms
retrive time: 4089 ms
retrive time: 5255 ms
retrive time: 5708 ms
retrive time: 6485 ms
retrive time: 6916 ms
retrive time: 7421 ms
retrive time: 8139 ms

这是我的代码(注意:httpwebrequest keepalive已关闭):

     static void Main()
    {

        for (int i = 0; i < 10; i++)
        {
            HttpWebRequest_BeginGetResponse req = new HttpWebRequest_BeginGetResponse();
            new Thread(new ThreadStart(req.start))
            {
                IsBackground=true

            }.Start();


        }
        Console.ReadLine();   
    }

这里是我放秒表的地方

            IAsyncResult result =
              (IAsyncResult)myHttpWebRequest.BeginGetResponse(new AsyncCallback(RespCallback), myRequestState);
            ThreadPool.RegisterWaitForSingleObject(result.AsyncWaitHandle, new WaitOrTimerCallback(TimeoutCallback), myHttpWebRequest, DefaultTimeout, true);
            Stopwatch wtch = new Stopwatch();
            wtch.Start();
            allDone.WaitOne();
            wtch.Stop();
            Debug.WriteLine("retrive time: "+wtch.ElapsedMilliseconds+" ms");

这样做的正确方法是什么?

0 个答案:

没有答案
相关问题