我在下载使用System.Net.WebClient.DownloadFileAsync文件()。用于使用异步版本的唯一原因是显示下载的进度。 我的code的执行可能无法继续100%,已经达到了。目前我使用一个忙等待(见code),但我不知道是否有一个更聪明的方法来做到这一点。
使用(WebClient的oWebClient =新的Web客户端())
{
oWebClient.Encoding = System.Text.Encoding.UTF8;
长lReceived = 0;
长LTOTAL = 0;
//设置一个委托观看下载进度。
oWebClient.DownloadProgressChanged + =委托(对象发件人,DownloadProgressChangedEventArgs E)
{
Console.WriteLine(e.ProgressPercentage +%(+ e.BytesReceived / 1024f + e.TotalBytesToReceive / 1024f +KB)的KB);
//分配给外部变量,允许忙等待检查。
lReceived = e.BytesReceived;
LTOTAL = e.TotalBytesToReceive;
};
oWebClient.DownloadFileAsync(新的URI(SURL + sPostData),sTempFile?);
而(lReceived == 0 || lReceived!= LTOTAL)
{
//忙等待。
Thread.sleep代码(1000);
}
}
解决方案
使用(WebClient的oWebClient =新的Web客户端())
{
//使用一个事件的等待,而不是一个Thread.sleep()方法循环。
VAR通知=新的AutoResetEvent(假);
oWebClient.Encoding = System.Text.Encoding.UTF8;
长lReceived = 0;
长LTOTAL = 0;
//设置一个委托观看下载进度。
oWebClient.DownloadProgressChanged + =委托(对象发件人,DownloadProgressChangedEventArgs E)
{
Console.WriteLine(e.ProgressPercentage +%(+ e.BytesReceived / 1024f + e.TotalBytesToReceive / 1024f +KB)的KB);
//分配给外部变量,允许忙等待检查。
lReceived = e.BytesReceived;
LTOTAL = e.TotalBytesToReceive;
//表明,做事情
如果(lReceived> = LTOTAL)notifier.Set();
};
oWebClient.DownloadFileAsync(新的URI(SURL + sPostData),sTempFile?);
//等待信号进行
notifier.WaitOne();
}
I'm downloading a file using System.Net.WebClient.DownloadFileAsync(). The only reason for using the async version is to show the progress of the download. My code execution may not continue before 100% have been reached. Currently I'm using a busy-wait (see code) but I wonder if there is a smarter way to do it.
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);
}
}
解决方案
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();
}
上一篇:的WebView Android 4.0的文件上传文件上传、WebView、Android
下一篇:java.lang.RuntimeException的:eglSwapBuffers失败:EGL_SUCCESS报告报告、lang、java、RuntimeException