简体   繁体   English

WebClient DownloadFileAsync - 如何向用户显示下载速度?

[英]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.

相关问题 如何使用WebClient DownloadFileAsync解决某些用户的下载问题? - How to fix download issues for some users using WebClient DownloadFileAsync? WebClient.DownloadFileAsync - 一次下载一个文件 - WebClient.DownloadFileAsync - Download files one at a time 如何在WebClient.DownloadFileAsync上实现超时 - How to implement a Timeout on WebClient.DownloadFileAsync 如何为WebClient.DownloadFileAsync捕获404 WebException - How to catch 404 WebException for WebClient.DownloadFileAsync WebClient.DownloadFileAsync方法:如何检索对象? - WebClient.DownloadFileAsync Method : How to retrieve the Object? 如何将 Webclient.DownloadFileAsync 包装成可等待的方法? - How to wrap Webclient.DownloadFileAsync into an awaitable method? 为什么我不能运行超过5个WebClient.DownloadFileAsync? - Why I can't have more than 5 WebClient.DownloadFileAsync running? WebClient.DownloadFileAsync()在慢速网络上无法正确下载文件 - WebClient.DownloadFileAsync() does not download files correctly on slow network 使用webClient.DownloadFileAsync无需单击按钮即可下载和保存文件 - Use webClient.DownloadFileAsync to download and save file without clicking button WebClient.DownloadFileAsync-下载多个文件时,如何跟踪下载的字节总数? - WebClient.DownloadFileAsync - How do I keep track of the total amount of bytes downloaded when downloading multiple files?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM