[英]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.