【发布时间】:2011-12-11 16:00:02
【问题描述】:
我正在尝试创建自定义下载应用。除了无法从“DownloadProgressChangedEventArgs”中获取“percent1”变量的下载所有按钮之外,它都可以正常工作。我已经在 mainForm 构造函数之前实例化了它,但它不会读取更改的值。
这是代码,由于大部分与问题无关,因此被部分剥离:
public partial class Main : Form
{
//Variables (not all, just the one im having issues with)
private double percentage1;
//Main form constructor
public Main(){...}
//Download File Async custom method
public void DldFile(string url, string fileName, string localPath, AsyncCompletedEventHandler completedName, DownloadProgressChangedEventHandler progressName)
{
WebClient webClient = new WebClient();
webClient.DownloadFileAsync(new Uri(url), localPath + "\\" + fileName);
webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(completedName);
webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(progressName);
}
//Button 1 click event to start download
private void btnDld1_Click(object sender, EventArgs e)
{
if (url1 != "" && Directory.Exists(localPath1))
{
_startDate1 = DateTime.Now;
DldFile(url1, fileName1, localPath1, completed1, progress1);
}
//took out the try/catch, other ifs to try and cut it down
}
//Download Progress Changed event for Download 1
public void progress1(object sender, DownloadProgressChangedEventArgs e)
{
percentage1 = e.ProgressPercentage; //THIS IS WHERE I WAS EXPECTING TO UPDATE "percentage1"
progressBar1.Value = int.Parse(Math.Truncate(percentage1).ToString());
}
//Button that starts all downloads click event where all my problems are at the moment
private void btnDldAll_Click(object sender, EventArgs e)
{
//The progress bar that should let me know the global status for all webClients
progressBarAll.Value = (
int.Parse(Math.Truncate(percentage1).ToString()) + //HERE IS MY PROBLEM
int.Parse(Math.Truncate(percentage2).ToString()) + //HERE IS MY PROBLEM
int.Parse(Math.Truncate(percentage3).ToString()) + //HERE IS MY PROBLEM
int.Parse(Math.Truncate(percentage4).ToString()) + //HERE IS MY PROBLEM
int.Parse(Math.Truncate(percentage5).ToString())) / 5; //HERE IS MY PROBLEM
//Checks if the link exists and starts it from the download button click event
if (url1 != "")
{
btnDld1.PerformClick();
}
//Continues for url2, 3, 4, 5 and else
}
}
所以这是我找到的让你知道我想要完成什么的最短方法,如果有什么遗漏请告诉我,我会尽快添加任何信息。
我试图实例化“progress1”来尝试访问它的百分比1 变量,但它没有工作。我试过用 webClient 做同样的事情,但也没有用。我使用谷歌和堆栈流搜索无济于事。所以我不确定这个问题是否太愚蠢,或者有一种不同的方式来看待这个完全超出我的思维方式的问题。
所以主要问题是更新“percentage1”变量并使用它。 当我掌握正确的值时,还有其他关于“progressBarAll.Value”计算的问题将得到解决。因此,如果您看到它,则无需担心。
【问题讨论】:
-
嗯,这是一个好问题,我不想添加它,所以这篇文章只针对一个问题,然后我会尝试进一步解决这个问题。但我们的想法是根据正在运行的任何下载来保持
progressBarAll的更新。如果 dld1 正在进行,那么它将显示 download1 进度,如果超过 1 个正在工作,它将显示所有工作下载的平均值。但我想一旦我在事件处理程序之外获得 e.Percentage 值的句柄,我就可以完成它。 -
它到底是怎么不工作的,DownloadProgressChanged 事件是否从未触发,或者变量从未分配?尝试在
progress1()上放置一个断点,看看它是否被命中。然后逐步调试它以追踪实际问题。据我所知,您在这里没有做错任何事情,您应该能够从事件处理程序中设置 percent1 而不会出现问题。 -
@Aevitas - 好吧,我按照你说的设置了断点,我看到的让我更加困惑,发生了一些奇怪的事情。当我单击下载时,所有事件处理程序都会被触发。但是变量
percentage1的值与e.ProgressPercentage事件参数不同,即使我在progress1()中声明percentage1 = e.ProgressPercentage。而且progressBarAll 根本不动,即使我可以看到percentage*的值都在变化。 -
这很奇怪,因为你不习惯编程。您似乎将您的代码视为数学表达式。您期望一旦编写了平均进度的代码,该关系应该永远保持。但是,c# 是imperative programming language。这只是机器的一系列命令:执行此操作,然后执行此操作。因此,平均仅在您单击
btnDldAll时发生。有不同的技术可以让它像数学函数一样工作。我在回答中提出了其中一个。 -
嗯,我学习 C# 的主要问题是改变我的思维方式。编程思想与我过去所做的完全不同。这是一个相当大的挑战,但我会掌握它,因为 StackOverflow 有一个很棒的社区,可以指出什么是错误的以及如何正确地做。感谢 Aevitas 的帮助。