我需要下载大量的文件从网络上基于一个关键字。我所遵循的步骤是
创建一个线程来下载每个文件以获得更好的性能是一个好主意吗?任何建议。谢谢
foreach (string each in arr)
{
Thread t = new Thread(
new ThreadStart(
delegate
{
string[] arr2 = each.Split(new string[] { "http://" }, StringSplitOptions.None);
string[] firstElem = arr2[1].Split(new string[] { " " }, StringSplitOptions.None);
string urlToDownload = @firstElem[0].Replace("\"", string.Empty);
string filName = Path.GetFileName(urlToDownload);
string dirName = DirInAppConfig();
DataRow row;
bool dataExistsInDtKwWithSameDownloadLinkAndFileName;
getRowForKwDownLinkFileName(urlToDownload, filName, out row, out dataExistsInDtKwWithSameDownloadLinkAndFileName);
downloadFile(Client, urlToDownload, dirName, filName, search, row);
}));
t.IsBackground = true;
t.Start();
t.Join();
}发布于 2010-07-21 10:15:37
通常,服务器限制从一个IP到2个连接的下载。因此,如果所有文件都来自同一台服务器,则多个线程可能帮助不大。
发布于 2010-07-21 10:19:43
您是否做过性能分析,向您指出需要考虑线程处理?不是吗?那么,您正在使用过早的优化,您应该立即停止这种情况。
您是否有多线程经验,这样您就不会在锁定方面犯一些愚蠢的错误,或者,如果您确实犯了这样的错误,您将能够迅速找到并修复它吗?不是吗?那你现在就该停下来。
您可能不清楚调试多线程程序需要多少时间。通过使用多个线程,时间可能会完全超过您可以节省的时间。
https://stackoverflow.com/questions/3298059
复制相似问题