简体   繁体   中英

Why i'm getting exception that the progressBar value is 104 on the second time i start download?

On the download button click event I reset the progressBar2 value and set the min and max to 0 and 100 respectively. In this case the List newList contain 9 items.

private void btnDownload_Click(object sender, EventArgs e)
{
    btnDownload.Enabled = false;
    label7.Text = "Downloading...";
    progressBar2.Value = 0;
    progressBar2.Minimum = 0;
    progressBar2.Maximum = 100;
    downloadFile(newList);
}

Then the download methods.

private Queue<string> _downloadUrls = new Queue<string>();

private async void downloadFile(IEnumerable<string> urls)
{
    foreach (var url in urls)
    {
        _downloadUrls.Enqueue(url);
    }

    await DownloadFile();
}

private async Task DownloadFile()
{
    if (_downloadUrls.Any())
    {
        WebClient client = new WebClient();
        client.DownloadProgressChanged += ProgressChanged;
        client.DownloadFileCompleted += Completed;

        var url = _downloadUrls.Dequeue();

        sw = Stopwatch.StartNew();
        await client.DownloadFileTaskAsync(new Uri(url), @"C:\Temp\TestingSatelliteImagesDownload\" + count + ".jpg");
        return;
    }
}

Then the progresschanged event

private void ProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
    // Calculate download speed and output it to labelSpeed.
    label3.Text = string.Format("{0} kb/s", (e.BytesReceived / 1024d / sw.Elapsed.TotalSeconds).ToString("0.00"));

    // Update the progressbar percentage only when the value is not the same.
    double bytesInCurrentDownload = double.Parse(e.BytesReceived.ToString());
    double totalBytesCurrentDownload = double.Parse(e.TotalBytesToReceive.ToString());
    double percentageCurrentDownload = bytesInCurrentDownload / totalBytesCurrentDownload * 100;
    ProgressBar1.Value = int.Parse(Math.Truncate(percentageCurrentDownload).ToString());//e.ProgressPercentage;

    // Show the percentage on our label.
    Label4.Text = e.ProgressPercentage.ToString() + "%";

    // Update the label with how much data have been downloaded so far and the total size of the file we are currently downloading
    label10.Text = string.Format("{0} MB's / {1} MB's",
    (e.BytesReceived / 1024d / 1024d).ToString("0.00"),
    (e.TotalBytesToReceive / 1024d / 1024d).ToString("0.00"));

    //Let's update ProgressBar2
    double totalBytesDownloaded = e.BytesReceived + bytesFromCompletedFiles;
    double percentageTotalDownload = totalBytesDownloaded / totalBytesToDownload * 100;
    progressBar2.Value = (int)percentageTotalDownload;
    label6.Text = progressBar2.Value.ToString() + "%";
    if (progressBar2.Value == 100)
    {
        label7.Text = "Download completed";
        btnDownload.Enabled = true;
        israelDownload = true;
        totalBytesCurrentDownload = 0;
        percentageTotalDownload = 0;
        progressBar2.Value = 0;
    }
}

I'm trying to reset the values here also to 0 when the progressBar2.Value is 100

And the completed event

long bytesFromCompletedFiles = 0;
// The event that will trigger when the WebClient is completed
private async void Completed(object sender, AsyncCompletedEventArgs e)
{
     if (e.Cancelled == true)
     {
          MessageBox.Show("Download has been canceled.");
     }
     else
     {
         if (e.Error == null)
         {
              ProgressBar1.Value = 100;
              count++;
              bytesFromCompletedFiles += totalBytes[count - 1];
              label9.Text = numberoffiles--.ToString();
              await DownloadFile();
         }
         else
         {
              string error = e.UserState.ToString();
         }
     }
     sw.Stop();
}

This is how I calculate the totalBytesToDownload variable I'm using this method helper:

long totalBytesToDownload = 0;
List<int> totalBytes;
private void getTotalBytes(List<string> urls)
{
     totalBytes = new List<int>();
     for (int i = 0; i < urls.Count(); i++)
     {
          System.Net.WebRequest req = System.Net.HttpWebRequest.Create(urls[i]);
          req.Method = "HEAD";
          using (System.Net.WebResponse resp = req.GetResponse())
          {
               int ContentLength;
               if (int.TryParse(resp.Headers.Get("Content-Length"), out ContentLength))
               {
                    //Do something useful with ContentLength here
                        totalBytes.Add(ContentLength);
               }
           }
      }
            totalBytes.ForEach(file => totalBytesToDownload += file);
}

And using this method in a backgroundworker dowork event this is before I'm starting the download:

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
     ei.Init(israelDownload);
     getTotalBytes(ExtractImages.imagesUrls);
}

imagesUrls is List and contain 9 items.

The first time i click the button and start download it's working fine. Once the progressBar2 value is getting to 100% i'm trying to click on the start button again to download the same files again. On the second time i click the button i'm getting exception on the line 150:

progressBar2.Value = (int)percentageTotalDownload;

Message=Value of '104' is not valid for 'Value'. 'Value' should be between 'minimum' and 'maximum'. Parameter name: Value ParamName=Value Source=System.Windows.Forms StackTrace: at System.Windows.Forms.ProgressBar.set_Value(Int32 value) at SatelliteImages.Form1.ProgressChanged(Object sender, DownloadProgressChangedEventArgs e)

I think the problem is the decimal separator (Cultures). You are using double.Parse and int.Parse . I think you are parsing a 10.4' to 104 _(because in some cultures the ,` is the decimal separator. (I see on your name you are Dutch) and you system might be dutch._

Change all Parse to casts.

double bytesInCurrentDownload = double.Parse(e.BytesReceived.ToString());

to

double bytesInCurrentDownload = (double)e.BytesReceived;

Also you are formating it to en-US -> e.BytesReceived / 1024d / 1024d).ToString("0.00"),


You could check the decimal separator :

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM