我正在下载一个包含许多域的文本文件:
private void bgWorker_DoWork(object sender, DoWorkEventArgs e)
{
// backgroundworker
string action = e.Argument as string;
if (action == "xc_download")
{
// this downloads the daily domains from the main site
// format the date to append to the url
DateTime theDate = DateTime.Now;
theDate.ToString("yyyy-MM-dd");
string downloadURL = String.Empty;
downloadURL = ("http://www.namejet.com/Download/" + (theDate.ToString("M-dd-yyyy") + ".txt"));
using (WebClient wc = new WebClient())
{
string urls = wc.DownloadString(downloadURL);
dgView.Rows.Add(urls);
}
} // if (action == "xc_download")
}
一旦下载,我就会尝试将它们添加到一个数据集中。问题是,这很慢。是否有更快的方法下载文本文件并将数据添加到我应该使用的网格视图中?
发布于 2015-05-25 06:21:26
您可以并行处理下载过程。一种选择是TPL的Parallel.ForEach
。您可以设置并发操作(下载)的最大数量,以防止服务器被淹没。
List<string> downloads = new List<string>();
downloads.Add(...);
Parallel.ForEach
( downloads
, new ParallelOptions() { MaxDegreeOfParallelism = 8 } /* max 8 downloads simultaneously */
, url => Download(url)
);
然后创建一个下载方法并在其中处理下载:
private void Download(string url)
{
// download
this.Invoke((MethodInvoker)delegate()
{
// update the UI inside here
});
}
https://stackoverflow.com/questions/30439946
复制相似问题