如何从DownloadStringCompleted事件中获取此错误?这是不是意味着,它已经完成了?还有其他事件可以解决这个问题吗?
我很少得到这个错误,但偶尔会发生在我的WP7手机上.我有一个Web客户端,我一遍又一遍地开火,然后我从已完成的事件中重新开火.这是否正在发生,因为仍然存在一些陈旧的连接?有没有办法防止这种100%?
我已经检查过线程是否有机会自己走过去,但它只是在已完成的事件中触发.
我怎么能确定,当整个事件被解雇时,客户端不再是isBusy?一个建议是在客户端忙的时候添加一段线程休眠.
一些伪代码.
var client = new WebClient("URL 1");
client.CompletedEvent += CompletedEvent;
client.downloadasync();
void CompletedEvent(){
Dosomestuff;
client.downloadasync(); //This is where we break.
}
Run Code Online (Sandbox Code Playgroud) 我使用下面的代码从TFS服务器下载多个附件:
foreach (Attachment a in wi.Attachments)
{
WebClient wc = new WebClient();
wc.Credentials = (ICredentials)netCred;
wc.DownloadFileCompleted += new AsyncCompletedEventHandler(wc_DownloadFileCompleted);
wc.DownloadFileAsync(a.Uri, "C:\\" + a.Name);
}
Run Code Online (Sandbox Code Playgroud)
我想使用DownloadFileAsync下载多个文件,但我希望逐个下载它们.
有人可能会问"你为什么不使用同步的DownloadFile方法?" 这是因为:
这是我想到的解决方案:
foreach (Attachment a in wi.Attachments)
{
WebClient wc = new WebClient();
wc.Credentials = (ICredentials)netCred;
wc.DownloadFileCompleted += new AsyncCompletedEventHandler(wc_DownloadFileCompleted);
wc.DownloadFileAsync(a.Uri, "C:\\" + a.Name);
while (wc.IsBusy)
{
System.Threading.Thread.Sleep(1000);
}
}
Run Code Online (Sandbox Code Playgroud)
但是,这种方法存在一些问题:
有没有更好的方法使用WebClient.DownloadFileAsync一次下载一个文件?
谢谢!