C# 为什么我';在我第二次开始下载时,progressBar的值是104,这是一个异常吗?
在下载按钮上,单击事件I重置progressBar2值,并将最小值和最大值分别设置为0和100。在本例中,列表newList包含9项C# 为什么我';在我第二次开始下载时,progressBar的值是104,这是一个异常吗?,c#,.net,winforms,C#,.net,Winforms,在下载按钮上,单击事件I重置progressBar2值,并将最小值和最大值分别设置为0和100。在本例中,列表newList包含9项 private void btnDownload_Click(object sender, EventArgs e) { btnDownload.Enabled = false; label7.Text = "Downloading..."; progressBar2.Value = 0; progressBar2.Minimum
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);
}
然后是下载方法
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;
}
}
当progressBar2.Value为100时,我尝试将此处的值重置为0
以及完成的活动
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();
}
这是我使用此方法计算TotalByTestToDownload变量的方式:
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);
}
imagesUrls是一个列表,包含9项
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);
}
第一次单击按钮并开始下载时,它工作正常。
一旦progressBar2值达到100%,我将再次尝试单击开始按钮以再次下载相同的文件。第二次单击按钮时,第150行出现异常:
progressBar2.Value = (int)percentageTotalDownload;
消息=值“104”对“值”无效“值”应介于“最小值”和“最大值”之间。
参数名称:Value
参数名=值
Source=System.Windows.Forms
堆栈跟踪:
在System.Windows.Forms.ProgressBar.set_值(Int32值)处
在SatelliteImages.Form1.ProgressChanged(对象发送者,下载ProgressChangedEventArgs e)
我认为问题在于十进制分隔符(文化)。您正在使用
double.Parse
和int.Parse
。我认为您正在将10.4'解析为104(因为在某些文化中,
,`是十进制分隔符。(我在您的名字上看到您是荷兰人)并且您的系统可能是荷兰人_
将所有解析更改为强制转换
double bytesInCurrentDownload = double.Parse(e.BytesReceived.ToString());
到
此外,您正在将其格式化为en US->e.BytesReceived/1024d/1024d).ToString(“0.00”),
您可以检查:您的Label7
是否在第一次运行代码时显示“下载完成”?这看起来很奇怪:int.Parse(Math.Truncate(percentageCurrentDownload.ToString())
您应该将其更改为:(int)percentageCurrentDownload
您应该使用从int到double再到back的转换,而不是字符串解析。也可能是“TotalBytesDownload/TotalByTestToDownload”看看它产生了104%的totalbytesdownload>ByTestToDownload。为什么要在ProgressChanged
和Completed
中检查两个不同的进度条?一个是progressBar
1,另一个是progressBar2
?@中间看起来第一个progressBar是每个文件的,第二个是所有文件的总数…我不确定这是问题所在。如果是的话,它可能在代码第一次运行时也会失败…@Pikoh-true,这可能更像是一个注释。。。但使用解析而不是强制转换可能会产生奇怪的结果。
double bytesInCurrentDownload = double.Parse(e.BytesReceived.ToString());
double bytesInCurrentDownload = (double)e.BytesReceived;