我正在使用带有CookieContainer的Webclient从Web服务(Sharepoint)下载数据。
我想要DownloadDataAsync,以便下载多个文档并在下载时为每个文档更新一个进度条。非异步版本 - DownloadData不会发送进度更新。
如何在不使用DoEvents的情况下将更改应用于进度条?
部分类Form() { void Main() { 列表urls = new List(); //urls.Add("xxxxxxx“); //从某个地方获取它们
for (int i = 0; i < urls.Count; i++)
{
var doc = new Document();
doc.BinaryData = DocumentAsArray(urls.ElementAt(i));
entities.AddToDocument(doc);
}
}
public byte[] DocumentAsArray(string URL)
{
byte[] @return = null;
using (CookieAwareWebClient client = new CookieAwareWebClient())
{
client.CookieContainer = spAuthentication.CookieContainer;
// Assign the events to capture the progress percentage
client.DownloadFileCompleted += new AsyncCompletedEventHandler(client_DownloadFileCompleted);
client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);
client.DownloadDataAsync(new Uri(URL));
}
return @return;
}
void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
progressBarControlFile.Position = e.ProgressPercentage;
var newPc = string.Format("Downloaded {0} %", e.ProgressPercentage);
labelControlFile.Text = newPc;
Application.DoEvents();
}
void client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
{
progressBarControlFile.Position = progressBarControlFile.Properties.Maximum;
labelControlFile.Text = string.Format("{0} %", progressBarControlFile.Properties.Maximum);
Application.DoEvents();
}
}
公共类CookieAwareWebClient:WebClient { 公共CookieContainer CookieContainer {get;组; }
protected override WebRequest GetWebRequest(Uri address)
{
WebRequest request = base.GetWebRequest(address);
var webRequest = request as HttpWebRequest;
if (webRequest != null)
{
webRequest.CookieContainer = this.CookieContainer;
webRequest.KeepAlive = false;
}
return request;
}
} }
答案 0 :(得分:1)
所以回顾一下,如果你只是想让异步获得进度更新,只需更改你的DocumentAsArray函数,等待1中所述的DownloadFileCompleted事件。那么你所要做的就是要小心你不要从另一个线程调用你的UI代码 - 你应该在Debug中得到一个异常,告诉你不要这样做(它在Release中运行良好 - 大部分时间都是如此)。所以使用InvokeEx调用。