c**t 发帖数: 2744 | 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);
}
} |
a9 发帖数: 21638 | 2 这叫异步执行吧?
【在 c**t 的大作中提到】 : 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) : {
|
c**d 发帖数: 579 | 3 This is not thread safety. And what issue does WebClient have? |
c**t 发帖数: 2744 | 4 Only two links were downloaded, app doesn't stop, doesn't download more
links either.
【在 c**d 的大作中提到】 : This is not thread safety. And what issue does WebClient have?
|
d****d 发帖数: 133 | 5 you create one instace of wc per task, wc doesn't need to be thread-safe.
【在 c**t 的大作中提到】 : Only two links were downloaded, app doesn't stop, doesn't download more : links either.
|
c**t 发帖数: 2744 | 6 read my code, each task has its own wc (using)
【在 d****d 的大作中提到】 : you create one instace of wc per task, wc doesn't need to be thread-safe.
|
v******n 发帖数: 421 | 7 how many cpu's (cores) in your machine? It is possible that TPL only
schedules
two threads
【在 c**t 的大作中提到】 : Only two links were downloaded, app doesn't stop, doesn't download more : links either.
|
d****d 发帖数: 133 | 8 that's what I meant. wc doesn't need to be thread-safe if you write code
like
this. If it doesn't work, it has nothing to do with wc's thread-safety.
【在 c**t 的大作中提到】 : read my code, each task has its own wc (using)
|
d****d 发帖数: 133 | 9 I just wrote a test app. I have no problem downloading multiple urls at all.
At peak I have 6 downloading simultaneously.
Something else is wrong in your project. But as I said, it shouldn't have
anything to do with wc's thread safety.
【在 d****d 的大作中提到】 : that's what I meant. wc doesn't need to be thread-safe if you write code : like : this. If it doesn't work, it has nothing to do with wc's thread-safety.
|
s*****w 发帖数: 215 | 10 I also tested the above code,
multiple files are downloaded,
wc.DownloadFile(url, filename); the second parameter is the filename you are
going to download to. For each thread, they should have their own file
names.
【在 c**t 的大作中提到】 : Only two links were downloaded, app doesn't stop, doesn't download more : links either.
|