当我尝试调用DownloadFileAsync时,程序崩溃并出现错误" WebClient不支持并发I / O操作。"我该怎么做才能解决这个问题?
WebClient klient = new WebClient();
MatchCollection matches = Regex.Matches(storage, pattern);
Match[] odkazy = new Match[matches.Count];
List<string> neco = new List<string> { };
for (int i=0;i<matches.Count;i++)
{
matches.CopyTo(odkazy, 0);
string ano = odkazy[i].ToString();
neco.Add(ano);
klient.DownloadFileAsync(new Uri(neco[i]), @"c:\picture{0}.png",i);
Debug.WriteLine(neco[i]);
}
答案 0 :(得分:0)
每次下载都需要使用新的WebClient实例;它一次只能做一个,而你正试图用你的for循环迅速做一堆。在每次迭代中初始化一个新的:
for (int i=0;i<matches.Count;i++)
{
matches.CopyTo(odkazy, 0);
string ano = odkazy[i].ToString();
neco.Add(ano);
WebClient klient = new WebClient();
klient.DownloadFileAsync(new Uri(neco[i]), @"c:\picture{0}.png",i);
Debug.WriteLine(neco[i]);
}
或者你可以&#34;链&#34;下载并在单个Web客户端的DownloadFileCompleted事件中启动下一个。