简体   繁体   English

如何在parallel.foreach中使用进度条?

[英]How to use progress bar with parallel.foreach?

I have number of files to be downloaded and I have used Parallel.foreach in C#. 我有许多文件要下载,并且在C#中使用了Parallel.foreach。 It is working fine. 一切正常。 Now I want to check the progress of download using progressbar. 现在,我想使用progressbar检查下载进度。 How is this possible? 这怎么可能?

I have used this code: 我使用了以下代码:

Parallel.For(0, numofitems, options, j =>
{
   using (WebClient client = new WebClient())
   {
       client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);

       client.DownloadFile(list[j], @"F:\Test\Test2\a" + j + ".png"); 
   }
}

Try this one. 试试这个。

1.Drop a progressbar into your form. 1.将进度条拖放到表单中。 In my example I leave the name it as is "progressBar1" 在我的示例中,我将其名称保留为“ progressBar1”

2.Add this change your DownloadProgressChanged event into the following 2.将此更改添加到您的DownloadProgressChanged事件中,如下所示

 client.DownloadProgressChanged += (s, e) =>
       {
        progressBar1.Value = e.ProgressPercentage;
       };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM