【发布时间】:2018-01-11 12:49:51
【问题描述】:
在我的应用中,我有一个带有链接的列表。
public List<string> Urls ()
{
var list = new List<string>();
list.Add("http://example.com/image.jpg");
list.Add("http://example.com/image1.jpg");
list.Add("http://example.com/image2.jpg");
return list;
}
我使用网络客户端进行下载。
private void downloadAlbum_Click(object sender, EventArgs e)
{
foreach (var link in Urls())
{
using (var wc = new WebClient())
{
wc.DownloadFile(link.ToString(),fileName);
}
}
}
但是,我的 winform 落后了。而 WebClient 做这项工作。 也许有人有一个具有多个下载的异步功能示例并在进度条中显示当前进度? 更新。 我有一个文件的异步等待
private void Downloader(string link, string filepath)
{
using (WebClient wc = new WebClient())
{
wc.DownloadProgressChanged += Wc_DownloadProgressChanged;
wc.DownloadFileCompleted += Wc_DownloadFileCompleted;
wc.DownloadFileAsync(new Uri(link), filepath);
}
}
private void Wc_DownloadProgressChanged(object sender,
DownloadProgressChangedEventArgs e)
{
progressBar.Value = e.ProgressPercentage;
}
private void Wc_DownloadFileCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
{
progressBar.Value = 0;
if (e.Cancelled)
{
MessageBox.Show("Canceled", "Message", MessageBoxButtons.OK,MessageBoxIcon.Error);
return;
}
if (e.Error != null)
{
MessageBox.Show("Somethings wrong, check your internet","Message", MessageBoxButtons.OK,MessageBoxIcon.Error);
return;
}
MessageBox.Show("Download is done!", "Message",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
【问题讨论】:
-
您是否尝试过使用
async却失败了?最好表现出你的一些努力,因为你似乎已经知道该做什么了。 -
从单独的线程下载,然后用调度器调整进度值
-
为什么要在列表中声明一个新列表然后返回它?它不会失去作用域吗?
-
任何可以提高我的编码技能的帮助都会对我有用:)
标签: c#