Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to Download a single file with multi-Threads in C# [closed]

I want to download a single file with Multi-Threads in C#. e.g: Thread1 start downloading from 1% to 40%. Thread2 start downloading from 41% to 70%. Thread3 start downloading from 71% to 100%

Please suggest me some code. Thanks in advance

like image 875
Rizwan Khan Avatar asked Nov 07 '11 12:11

Rizwan Khan


1 Answers

How about using HttpRequest class, with AddRange method called. This should a header with offset from which to start downloading.

    var request = HttpWebRequest.Create(new Uri("http://www.myurl.com/hugefile"));

    request.Method = "GET";
    request.AddRange(offset_for_this_thread);  // I assume you have calculated this
                                               // before firing threads
    Stream reponseStream = request.GetResponse().GetResponseStream();

You can then read from ´responseStream´ the data and merge it with the other threads once it is done.

However, as noticed by everybody else, this will only bring value if you have two adapters, both connected to internet, and you have some kind of bandwidth balancing between those adapter... Otherwise Windows wil likely divert everything to the same connection.

like image 162
zmilojko Avatar answered Oct 07 '22 13:10

zmilojko