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

Shalva Gabriel 61 Reputation points
2021-10-20T05:36:17.757+00:00

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();  
        }  
Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,821 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,203 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Jiale Xue - MSFT 30,811 Reputation points Microsoft Vendor
    2021-10-21T03:16:50.853+00:00

    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.

    0 comments No comments