[英]How to use progress bar with parallel.foreach?
我有许多文件要下载,并且在C#中使用了Parallel.foreach。 一切正常。 现在,我想使用progressbar检查下载进度。 这怎么可能?
我使用了以下代码:
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");
}
}
试试这个。
1.将进度条拖放到表单中。 在我的示例中,我将其名称保留为“ progressBar1”
2.将此更改添加到您的DownloadProgressChanged事件中,如下所示
client.DownloadProgressChanged += (s, e) =>
{
progressBar1.Value = e.ProgressPercentage;
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.