将某些DownloadProgressChangedEvent分配给progressbar [英] Assign certain DownloadProgressChangedEvent to progressbar

查看:166
本文介绍了将某些DownloadProgressChangedEvent分配给progressbar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,
我正在尝试创建一个简单的多下载应用程序,但在将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(使用



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!

推荐答案

您可以通过将sender参数传递给事件处理程序来区分不同的Web客户端.

但是您的代码看起来很糟糕.
这里有一些建议:

You can distinguish the different web clients with the sender parameter passed to your event handler.

But your code looks very bad.
Here some suggestions:

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆