问题描述
你好,
我正在尝试创建一个简单的多下载应用程序,但在将DownloadProgressChangedEvent分配给某个进度条时遇到了一个小问题.
我正在使用以下代码创建所需的Web客户端等:
Hi there,
I´m trying to create a simple multi-download-application but I´ve got a small problem with assigning a DownloadProgressChangedEvent to a certain progressbar.
I´m using this code to create the needed webclients, etc:
for (int z = 0; z < CheckedCount; z++)
{
_MultipleWebClients = new WebClient();
_MultipleWebClients.DownloadFileCompleted += new AsyncCompletedEventHandler(_DownloadFileCompleted);
_MultipleWebClients.DownloadProgressChanged += new System.Net.DownloadProgressChangedEventHandler(_DownloadProgressChanged);
_MultipleWebClients.DownloadFileAsync(new Uri(_downloadUrlList[z].ToString()), @"F:\test" + z + ".mp4");
richTextBox1.AppendText(_downloadUrlList[z].ToString());
}
为了显示下载进度,我使用的是带有预定义文件名,URL和每行中带有进度条的datagridview(使用代码).
现在,我真的不知道如何分别分配每个DownloadProgressChangedEvent,以便每个进度条都显示正确的downloadprogress.
现在,所有进度条都显示相同的百分比,这可能是因为在我的代码中,它们都使用相同的downloadevent ...
To display the downloadprogress, I´m using a datagridview with predefined filenames, urls and with a progressbar in each row (with this code).
Now I really don´t know how to assign each DownloadProgressChangedEvent individually, so that each progressbar is showing the correct downloadprogress.
Right now, all progressbars are showing the same percentage, probably because, with my code, they´re all using the same downloadevent...
Thanks in advance!
推荐答案
private WebClient StartDownloadFile(Uri source, string targetFilename)
{
WebClient wc = new WebClient();
wc.DownloadFileCompleted += DownloadFileCompleted; //without "_"
wc.DownloadProgressChanged += DownloadProgressChanged;
wc.DownloadFileAsync(source, targetFilename);
return wc;
}
private Dictionary<string, WebClient> webClients = new Dictionary<string, WebClient>();
private void DownloadMultipleFiles(string[] sourceFiles)
{
for (int i = 0; i < sourceFiles.Length - 1; i++)
{
webClients[sourceFiles[i]] = StartDownloadFile(
new Uri(sourceFiles[i]), @"F:\test" + i + ".mp4");
rtbUrls.AppendText(sourceFiles[z]);
//Not richTextBox1 -> rtbUrls is a lot better
}
}
private void DownloadProgressChanged(object sender, EventArgs args) //sth. like this
{
WebClient wc = (WebClient)sender;
string url = webClients[wc];
//Update the corresponding progressbar
}
顺便说一句:
如果您想显示更多进度详细信息(例如平均速度),则应阅读此文章:
使用进度报告复制流 [ ^ ]
BTW.:
If you want to display more progress details (e.g. average speed), you should read this article:
Copy a Stream with Progress Reporting[^]
这篇关于将某些DownloadProgressChangedEvent分配给progressbar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!