avatar
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);
}
}
avatar
a9
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)
: {

avatar
c*d
3
This is not thread safety. And what issue does WebClient have?
avatar
c*t
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?
avatar
d*d
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.

avatar
c*t
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.
avatar
v*n
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.

avatar
d*d
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)
avatar
d*d
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.

avatar
s*w
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.

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。