WebClient is not thread-safe!# DotNet - 窗口里的风景
c*t
1 楼
Easy to test:
var links = new ConcurrentQueue(); //and populate with links
Parallel.For(0, link.Count, delegate(int i){
string url;
if( links.TryDeQueue(out url) )
{ Download(url); }
});
...
private void Download(string url)
{
using(WebClient wc = new WebClient())
{
wc.DownloadFile(url, somefilename);
}
}
var links = new ConcurrentQueue
Parallel.For(0, link.Count, delegate(int i){
string url;
if( links.TryDeQueue(out url) )
{ Download(url); }
});
...
private void Download(string url)
{
using(WebClient wc = new WebClient())
{
wc.DownloadFile(url, somefilename);
}
}