0

I use this code to download a file from a url.

Stream stm = myHttpResponse.GetResponseStream();
byte[] buff = new byte[4096];
Stream fs = new FileStream("c:\\file1.txt", FileMode.Append , FileAccess.Write);
int r = 0;

while((r = stm.Read(buff, 0, buff.Length)) > 0)
{
    fs.Write(buff, 0, r);
}

If I want to download 20 files (from different urls) simultaneously it's possible to do it with less than 20 threads?

Edit

HttpWebResponse hasn't async method. I was hoping some example with BeginRead/BeginWrite of streams. I think they dont consume threads from Threadpool

albert
  • 1,493
  • 1
  • 15
  • 33
  • Did you refer to : http://stackoverflow.com/questions/15276158/infinite-loop-while-downloading-multiple-files-with-webclient/15276809#15276809 ? Both the question and Eric's suggestion would help you to choose the right way. – Siva Gopal Mar 08 '13 at 08:53

2 Answers2

1

You can use the Task Parallel Library (TPL) for that. And set the Degree of Parallelism. On your scenario. Set it to 19.

Freddie Fabregas
  • 1,162
  • 1
  • 7
  • 17
0

No, it's impossible to have 20 simultaneous download streams in less than 20 threads. You could use a ThreadPool.QueueUserWorkItem and limit thread's count where, but that is not simultaneous IMO. Anyway you are better to use the WebClient class and its DownloadFileAsync method.

WebClient client = new WebClient();
client.Credentials = new NetworkCredential("username", "password");
client.DownloadFileAsync(uriString,fileName);
VladL
  • 12,769
  • 10
  • 63
  • 83