Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 如何为并行下载显示单个ProgressBar?_.net - Fatal编程技术网

.net 如何为并行下载显示单个ProgressBar?

.net 如何为并行下载显示单个ProgressBar?,.net,.net,我正在使用网络客户端,同时下载4个文件。这些文件是并行下载的,我可以在文件夹中看到文件大小的变化。现在我想显示每个文件的进度条。这是我到目前为止所做的尝试,但没有取得进展。有人能帮忙吗 private void Button_Click(object sender, RoutedEventArgs e) { System.Net.ServicePointManager.DefaultConnectionLimit = 10; List&l

我正在使用网络客户端,同时下载4个文件。这些文件是并行下载的,我可以在文件夹中看到文件大小的变化。现在我想显示每个文件的进度条。这是我到目前为止所做的尝试,但没有取得进展。有人能帮忙吗

 private void Button_Click(object sender, RoutedEventArgs e)
    {
            System.Net.ServicePointManager.DefaultConnectionLimit = 10;
            List<String> files = new List<String>();
            String localPath = "";
            files.Add(file1);
            files.Add(file2);
            files.Add(file3);
            files.Add(file4);

            foreach (String fi in files)
            {
                localPath = "C:\\Download\\" + fi.Split('/')[5].ToString();
                WebClient client = new WebClient();

                client.DownloadProgressChanged += (sender1, e1) => client_DownloadProgressChanged(sender1, e1, z);

                client.DownloadFileAsync(new Uri(fi), localPath, count);
                count += 1;
            }
     }

void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e, int RowNumber)
        {
            ProgressBar[] pb = new ProgressBar[4];

            pb[RowNumber-1] = new ProgressBar();
            pb[RowNumber-1].Width = 400;
            pb[RowNumber-1].Height =20;
            pnl.Children.Add(pb[RowNumber-1]);
            pb[RowNumber-1].Value = e.ProgressPercentage;
        }
private void按钮\u单击(对象发送者,路由目标)
{
System.Net.ServicePointManager.DefaultConnectionLimit=10;
列表文件=新列表();
字符串localPath=“”;
文件。添加(文件1);
文件。添加(文件2);
文件。添加(文件3);
文件。添加(文件4);
foreach(文件中的字符串fi)
{
localPath=“C:\\Download\\”+fi.Split('/')[5].ToString();
WebClient客户端=新的WebClient();
client.DownloadProgressChanged+=(发送方1,e1)=>client_DownloadProgressChanged(发送方1,e1,z);
DownloadFileAsync(新Uri(fi)、localPath、count);
计数+=1;
}
}
void client_DownloadProgressChanged(对象发送方,DownloadProgressChangedEventArgs e,int RowNumber)
{
ProgressBar[]pb=新的ProgressBar[4];
pb[RowNumber-1]=新的ProgressBar();
pb[RowNumber-1]。宽度=400;
pb[RowNumber-1]。高度=20;
pnl.Children.Add(pb[RowNumber-1]);
pb[RowNumber-1]。值=e.ProgressPercentage;
}

这里有多个选项。最好的解决办法是

  • 为每个文件名使用
    DownloadProgressChangedEventHandler
    方法中的附加参数。 看看你是怎么做到的
  • 使用不同的
    DownloadProgressChangedEventHandler
  • 许多其他选项,例如,
    Parallel.ForEach
  • 第一个解决方案代码(未测试-我现在无法访问IDE)

    List files=newlist();
    List pbs=新列表();
    私有无效按钮\u单击(对象发送者,路由目标e)
    {
    System.Net.ServicePointManager.DefaultConnectionLimit=10;
    字符串localPath=“”;
    文件。添加(文件1);
    文件。添加(文件2);
    文件。添加(文件3);
    文件。添加(文件4);
    foreach(文件中的字符串fi)
    {
    ProgressBar pb=新的ProgressBar();
    pb.宽度=400;
    pb.高度=20;
    pnl.儿童。添加(pb);
    添加(pb);
    localPath=“C:\\Download\\”+fi.Split('/')[5].ToString();
    WebClient客户端=新的WebClient();
    client.DownloadProgressChanged+=(发送方1,e1)=>client\u DownloadProgressChanged(发送方1,e1,fi);
    DownloadFileAsync(新Uri(fi)、localPath、count);
    计数+=1;
    }
    }
    void client_DownloadProgressChanged(对象发送方、DownloadProgressChangedEventArgs e、字符串fi)
    {       
    int pbNumber=files.IndexOf(fi);
    pbs[pbNumber]。值=e.ProgressPercentage;
    }
    
    Ok我替换了行以传递附加参数,但是它给出了错误client.DownloadProgressChanged+=(发送方,e)=>client\u DownloadProgressChanged(发送方,e,z);无法在此范围内声明局部变量发送者“无法在此范围内声明局部变量发送者”是因为您有第一个方法
    按钮\u单击
    参数名为sender。只需更改任何重复变量namesouch.的名称。。是的,现在它显示了进度,但没有更新。你真的这么做了吗?你从我提供的链接中了解到了什么吗?您的方法中有行吗?你想达到什么目的?我将为你更改代码,但只是为了让你知道你不会通过这种方式学到任何东西。我有你的代码,但现在它甚至没有下载。。0KB的文件,它不会影响DownloadProgressChanged方法。
    
    List<String> files = new List<String>();
    List<ProgressBar> pbs = new List<ProgressBar>();
    
    private void Button_Click(object sender, RoutedEventArgs e)
    {
        System.Net.ServicePointManager.DefaultConnectionLimit = 10; 
        String localPath = "";
        files.Add(file1);
        files.Add(file2);
        files.Add(file3);
        files.Add(file4);   
    
        foreach (String fi in files)
        {
            ProgressBar pb = new ProgressBar();
            pb.Width = 400;
            pb.Height =20;
            pnl.Children.Add(pb);
            pbs.add(pb);
    
            localPath = "C:\\Download\\" + fi.Split('/')[5].ToString();
            WebClient client = new WebClient();
    
            client.DownloadProgressChanged += (sender1, e1) => client_DownloadProgressChanged(sender1, e1, fi);
    
            client.DownloadFileAsync(new Uri(fi), localPath, count);
            count += 1;
        }
    }
    
    
    void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e, string fi)
    {       
        int pbNumber = files.IndexOf(fi);           
        pbs[pbNumber].Value = e.ProgressPercentage;
    }