我有一个简单的课程:
public class DownloadFile
{
...
public string GetFile(string fileUrl, string pathOut)
{
using (WebClient wc = new WebClient())
{
wc.DownloadFile(new Uri(fileUrl), pathOut);
return pathOut;
}
}
}我从BackgroundWorker中调用它2次,因为这个过程是下载和安装2个文件(在这里执行自定义安装程序)。
问题是,第一个文件下载和安装正常,但是第二个文件挂在wc2.DownloadFile(new Uri(fileUrl), pathOut);行上,永远不会从那里出来!
每次使用using时,我都会释放WebClient,所以我可以说:
// Created BackgroundWorker so the UI doesn't get blocked and I can
// can show the progress in a log...
BackgroundWorker bkWrk = new BackgroundWorker();
bkWrk.WorkerReportsProgress = true;
bkWrk.ProgressChanged += (s, e) =>
{
ReportProgress(String.Format("Progress: {0}%", e.ProgressPercentage));
};
bkWrk.DoWork = delegate {
DownloadFile fileManager = new DownloadFile();
fileManager.GetFile("http://domain.com/file_A.zip", "C:\\TEMP\\file_a.zip");
fileManager.GetFile("http://domain.com/file_B.zip", "C:\\TEMP\\file_b.zip");
};
bkWrk.RunWorkerAsync();
while(bkWrk.IsBusy)
{
// let's wait but fire all events
Application.DoEvents();
}我看不出有什么问题..。但事实是,该文件挂在DownloadFile方法上,甚至尝试使用Microsoft符号在方法中导航,没有任何结果。
甚至在请求中添加了一个标头,但问题仍然存在。
wc.Headers["User-Agent"] = "Mozilla/4.0 (Compatible; Windows NT 5.1; MSIE 6.0)" +
" (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";我是不是漏掉了什么明显的东西?
发布于 2011-10-06 12:30:33
DownloadFile fileManager =新DownloadFile();fileManager.GetFile("http://domain.com/file_A.zip","C:\TEMP\file_a.zip");fileManager.GetFile("http://domain.com/file_B.zip"“,"C:\TEMP\file_b.zip");
您知道wc.DownloadFile不是异步方法,对吗?这意味着后台工作人员必须先下载第一个文件,然后再下载第二个文件,然后才能完成工作。
它确实..。更大的问题是,有时下载这两个文件时都没有问题:-/ -
这意味着您等待了足够长的时间来完成这两个操作。
https://stackoverflow.com/questions/7673820
复制相似问题