[英]WebClient DownloadFileAsync - How can I display download speed to the user?
That's pretty much the whole question in the title.这几乎就是标题中的整个问题。 I have a WPF C# Windows application, I download files for the user and now want to display the speed.
我有一个 WPF C# Windows 应用程序,我为用户下载文件,现在想显示速度。
mWebClient.DownloadProgressChanged += (sender, e) => progressChanged(e.BytesReceived);
//...
DateTime lastUpdate;
long lastBytes = 0;
private void progressChanged(long bytes)
{
if (lastBytes == 0)
{
lastUpdate = DateTime.Now;
lastBytes = bytes;
return;
}
var now = DateTime.Now;
var timeSpan = now - lastUpdate;
var bytesChange = bytes - lastBytes;
var bytesPerSecond = bytesChange / timeSpan.Seconds;
lastBytes = bytes;
lastUpdate = now;
}
And do whatever you need with the bytesPerSecond variable.并使用 bytesPerSecond 变量做任何你需要的事情。
We can do this easily by determining how many seconds have passed since the download is started.我们可以通过确定自下载开始以来经过了多少秒来轻松地做到这一点。 We can divide BytesReceived value from total seconds to get the speed.
我们可以将 BytesReceived 值从总秒数中除以得到速度。 Take a look at the following code.
看看下面的代码。
DateTime _startedAt;
WebClient webClient = new WebClient();
webClient.DownloadProgressChanged += OnDownloadProgressChanged;
webClient.DownloadFileAsync(new Uri("Download URL"), "Download Path")
private void OnDownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
if (_startedAt == default(DateTime))
{
_startedAt = DateTime.Now;
}
else
{
var timeSpan = DateTime.Now - _startedAt;
if (timeSpan.TotalSeconds > 0)
{
var bytesPerSecond = e.BytesReceived / (long) timeSpan.TotalSeconds;
}
}
}
Use the DownloadProgressChanged event使用DownloadProgressChanged 事件
WebClient client = new WebClient ();
Uri uri = new Uri(address);
// Specify that the DownloadFileCallback method gets called
// when the download completes.
client.DownloadFileCompleted += new AsyncCompletedEventHandler (DownloadFileCallback2);
// Specify a progress notification handler.
client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(DownloadProgressCallback);
client.DownloadFileAsync (uri, "serverdata.txt");
private static void DownloadProgressCallback(object sender, DownloadProgressChangedEventArgs e)
{
// Displays the operation identifier, and the transfer progress.
Console.WriteLine("{0} downloaded {1} of {2} bytes. {3} % complete...",
(string)e.UserState,
e.BytesReceived,
e.TotalBytesToReceive,
e.ProgressPercentage);
}
When you hook up the WebClient, you can subsribe to the ProgressChanged event, eg当您连接 WebClient 时,您可以订阅 ProgressChanged 事件,例如
_httpClient = new WebClient();
_httpClient.DownloadProgressChanged += DownloadProgressChanged;
_httpClient.DownloadFileCompleted += DownloadFileCompleted;
_httpClient.DownloadFileAsync(new Uri(_internalState.Uri), _downloadFile.FullName);
The EventArgs for this handler give you the BytesReceieved and TotalBytesToReceive.此处理程序的 EventArgs 为您提供 BytesReceieved 和 TotalBytesToReceive。 Using this information, you should be able to determine the download speed and shot a progress bar accordingly.
使用此信息,您应该能够确定下载速度并相应地拍摄进度条。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.