question

ShalvaGabriel-1621 avatar image
0 Votes"
ShalvaGabriel-1621 asked JialeXueMSFT-4441 published

Why when downloading files the second file when downloading the speed label show the infinity sign?

In the screenshot the Download Speed = show the infinity sign kb/s instead the speed.

141981-sec1.jpg

The first downloading file it's showing fine the speed but then from the second it's showing the infinity sign. How can I fix it so it will show the speed for each file ?

The downloading method :

 private async Task DownloadAsync()
         {
             using (var client = new WebClient())
             {
                 client.DownloadFileCompleted += (s, e) => lblStatus.Text = "Download File Completed.";
                 client.DownloadFileCompleted += (s, e) => sw.Reset();
                 client.DownloadProgressChanged += (s, e) => progressBar1.Value = e.ProgressPercentage;
                 client.DownloadProgressChanged += (s, e) => lblAmount.Text = FormatBytes(e.BytesReceived);
                 client.DownloadProgressChanged += (s, e) => lblSpeed.Text = string.Format("{0} kb/s", (e.BytesReceived / 1024d / sw.Elapsed.TotalSeconds).ToString("0.00"));
                 client.DownloadProgressChanged += (s, e) => lblDownloadSize.Text = Convert.ToInt64(client.ResponseHeaders["Content-Length"]).ToString();
                 client.DownloadProgressChanged += (s, e) =>
                 {
                     lblDownloadProgress.Text = "%" + e.ProgressPercentage.ToString();
                     lblDownloadProgress.Left = Math.Min(
                         (int)(progressBar1.Left + e.ProgressPercentage / 100f * progressBar1.Width),
                         progressBar1.Width - lblDownloadProgress.Width
                     );
                 };
                 for (int i = 0; i < urls.Count; i++)
                 {
                     await client.DownloadFileTaskAsync(new Uri(urls[i]), @"d:\satImages\img" + i + ".gif");
                 }
             }
         }

Format bytes method :

 private string FormatBytes(long bytes)
         {
             string[] Suffix = { "B", "KB", "MB", "GB", "TB" };
             int i;
             double dblSByte = bytes;
             for (i = 0; i < Suffix.Length && bytes >= 1024; i++, bytes /= 1024)
             {
                 dblSByte = bytes / 1024.0;
             }
    
             return String.Format("{0:0.00} {1}", dblSByte, Suffix[i]);
         }

And in a button click event starting it :

 private async void btnStart_Click(object sender, EventArgs e)
         {
             lblStatus.Text = "Downloading...";
             sw.Start();
             await DownloadAsync();
         }


dotnet-csharpwindows-forms
sec1.jpg (24.0 KiB)
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

The value of TotalSeconds is probably 0 - rework your calculation to use the millisecond resolution.

0 Votes 0 ·

1 Answer

JialeXueMSFT-4441 avatar image
0 Votes"
JialeXueMSFT-4441 answered JialeXueMSFT-4441 published

Hi ShalvaGabriel-1621,

The whole process of the stopwatch is:
1. reset
2. start
3. stop

You did not stop and reset the stopwatch after the first download.

After modification:

private async Task DownloadAsync() {
    using (var client = new WebClient()) {
        client.DownloadFileCompleted += (s, e) => lblStatus.Text = "Download File Completed.";
        client.DownloadProgressChanged += (s, e) => progressBar1.Value = e.ProgressPercentage;
        client.DownloadProgressChanged += (s, e) => lblAmount.Text = FormatBytes(e.BytesReceived);
        client.DownloadProgressChanged += (s, e) => lblSpeed.Text = string.Format("{0} kb/s", (e.BytesReceived / 1024d / sw.Elapsed.TotalSeconds).ToString("0.00"));
        client.DownloadProgressChanged += (s, e) => lblDownloadSize.Text = Convert.ToInt64(client.ResponseHeaders["Content-Length"]).ToString();
        client.DownloadProgressChanged += (s, e) =>
        {
            lblDownloadProgress.Text = "%" + e.ProgressPercentage.ToString();
            lblDownloadProgress.Left = Math.Min(
                (int)(progressBar1.Left + e.ProgressPercentage / 100f * progressBar1.Width),
                progressBar1.Width - lblDownloadProgress.Width
            );
        };
        for (int i = 0; i < urls.Count; i++) {
            sw.Reset();
            sw.Start();
            await client.DownloadFileTaskAsync(new Uri(urls[i]), @"d:\satImages\img" + i + ".gif");
            client.DownloadFileCompleted += (s, e) => sw.Stop();
        }
    }
}


private async void BtnStart_Click(object sender, EventArgs e) {
    lblStatus.Text = "Downloading...";
    await DownloadAsync();
}

Output:
142312-output.gif

Best regards,
Jiale Xue



If the answer is the right solution, please click Accept Answer and kindly upvote it. If you have extra questions about this answer, please click Comment.

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.



output.gif (97.0 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.