Reputation: 1571
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
Upvotes: 0
Views: 411
Reputation: 13033
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);
Upvotes: 0
Reputation: 1203
You can use the Task Parallel Library
(TPL) for that. And set the Degree of Parallelism
. On your scenario. Set it to 19.
Upvotes: 1