我正在使用System.Net.WebClient.DownloadFileAsync()下载文件.使用异步版本的唯一原因是显示下载的进度.在达到100%之前,我的代码执行可能无法继续.目前我正在使用忙等待(见代码),但我想知道是否有更聪明的方法来做到这一点.
using(WebClient oWebClient = new WebClient())
{
oWebClient.Encoding = System.Text.Encoding.UTF8;
long lReceived = 0;
long lTotal = 0;
// Set up a delegate to watch download progress.
oWebClient.DownloadProgressChanged += delegate(object sender, DownloadProgressChangedEventArgs e)
{
Console.WriteLine(e.ProgressPercentage + "% (" + e.BytesReceived / 1024f + "kb of " + e.TotalBytesToReceive / 1024f + "kb)");
// Assign to outer variables to allow busy-wait to check.
lReceived = e.BytesReceived;
lTotal = e.TotalBytesToReceive;
};
oWebClient.DownloadFileAsync(new Uri(sUrl + "?" + sPostData), sTempFile);
while(lReceived == 0 || lReceived != lTotal)
{
// Busy wait.
Thread.Sleep(1000);
}
}
Run Code Online (Sandbox Code Playgroud)
using(WebClient oWebClient = new WebClient())
{
// use an event for waiting, rather than a Thread.Sleep() loop.
var notifier = new AutoResetEvent(false);
oWebClient.Encoding = System.Text.Encoding.UTF8;
long lReceived = 0;
long lTotal = 0;
// Set up a delegate to watch download progress.
oWebClient.DownloadProgressChanged += delegate(object sender, DownloadProgressChangedEventArgs e)
{
Console.WriteLine(e.ProgressPercentage + "% (" + e.BytesReceived / 1024f + "kb of " + e.TotalBytesToReceive / 1024f + "kb)");
// Assign to outer variables to allow busy-wait to check.
lReceived = e.BytesReceived;
lTotal = e.TotalBytesToReceive;
// Indicate that things are done
if(lReceived >= lTotal) notifier.Set();
};
oWebClient.DownloadFileAsync(new Uri(sUrl + "?" + sPostData), sTempFile);
// wait for signal to proceed
notifier.WaitOne();
}
Run Code Online (Sandbox Code Playgroud)
使用AutoResetEvent.在DownloadFileAsync()调用之后,在DownloadFileCompleted事件处理程序中调用其Set()方法,调用其WaitOne()方法.