gpt4 book ai didi

c# - 并行下载

转载 作者:太空狗 更新时间:2023-10-30 01:09:31 28 4
gpt4 key购买 nike

我正在尝试在C#中通过http实现文件的并行下载。我尝试了几种不同的方法,但它们似乎都无法正常工作。无论我做什么,下载最终都会排队并且无法以真正的并行方式工作。

谁能给我一些指导或链接到一篇描述实际有效方法的文章?

最佳答案

我只是写了一些代码,没有测试它,等待一些观察谢谢大家:

public class DownloadFile
{
public string Url { get; set; }

public string PathToSave { get; set; }
}


public class ParallelDownloading
{
private ConcurrentQueue<DownloadFile> _queueToDownlaod;
private IList<Task> _downloadingTasks;
private Timer _downloadTimer;

private int _parallelDownloads;

public ParallelDownloading(int parallelDownloads)
{
_queueToDownlaod = new ConcurrentQueue<DownloadFile>();
_downloadingTasks = new List<Task>();
_downloadTimer = new Timer();

_parallelDownloads = parallelDownloads;

_downloadTimer.Elapsed += new ElapsedEventHandler(DownloadTimer_Elapsed);
_downloadTimer.Interval = 1000;
_downloadTimer.Start();

ServicePointManager.DefaultConnectionLimit = parallelDownloads;
}

public void EnqueueFileToDownload(DownloadFile file)
{
_queueToDownlaod.Enqueue(file);
}

void DownloadTimer_Elapsed(object sender, ElapsedEventArgs e)
{
StartDownload();
}

private void StartDownload()
{
lock (_downloadingTasks)
{
if (_downloadingTasks.Count < _parallelDownloads && _queueToDownlaod.Count > 0)
{
DownloadFile fileToDownload;
if (_queueToDownlaod.TryDequeue(out fileToDownload))
{
var task = new Task(() =>
{
var client = new WebClient();
client.DownloadFile(fileToDownload.Url, fileToDownload.PathToSave);
}, TaskCreationOptions.LongRunning);

task.ContinueWith(DownloadOverCallback, TaskContinuationOptions.None);

_downloadingTasks.Add(task);
task.Start();
}
}
}
}

public void DownloadOverCallback(Task downloadingTask)
{
lock (_downloadingTasks)
{
_downloadingTasks.Remove(downloadingTask);
}
}
}

你可以用这个来测试它:

ParallelDownloading p = new ParallelDownloading(5);

p.EnqueueFileToDownload(new DownloadFile() { PathToSave = @"c:\file1.f", Url = @"http://download.thinkbroadband.com/20MB.zip" });
p.EnqueueFileToDownload(new DownloadFile() { PathToSave = @"c:\file2.f", Url = @"http://download.thinkbroadband.com/20MB.zip" });
p.EnqueueFileToDownload(new DownloadFile() { PathToSave = @"c:\file3.f", Url = @"http://download.thinkbroadband.com/20MB.zip" });
p.EnqueueFileToDownload(new DownloadFile() { PathToSave = @"c:\file4.f", Url = @"http://download.thinkbroadband.com/20MB.zip" });
p.EnqueueFileToDownload(new DownloadFile() { PathToSave = @"c:\file5.f", Url = @"http://download.thinkbroadband.com/20MB.zip" });
p.EnqueueFileToDownload(new DownloadFile() { PathToSave = @"c:\file6.f", Url = @"http://download.thinkbroadband.com/20MB.zip" });
p.EnqueueFileToDownload(new DownloadFile() { PathToSave = @"c:\file7.f", Url = @"http://download.thinkbroadband.com/20MB.zip" });
p.EnqueueFileToDownload(new DownloadFile() { PathToSave = @"c:\file8.f", Url = @"http://download.thinkbroadband.com/20MB.zip" });
p.EnqueueFileToDownload(new DownloadFile() { PathToSave = @"c:\file9.f", Url = @"http://download.thinkbroadband.com/20MB.zip" });
p.EnqueueFileToDownload(new DownloadFile() { PathToSave = @"c:\file10.f", Url = @"http://download.thinkbroadband.com/20MB.zip" });
p.EnqueueFileToDownload(new DownloadFile() { PathToSave = @"c:\file11.f", Url = @"http://download.thinkbroadband.com/20MB.zip" });
p.EnqueueFileToDownload(new DownloadFile() { PathToSave = @"c:\file12.f", Url = @"http://download.thinkbroadband.com/20MB.zip" });
p.EnqueueFileToDownload(new DownloadFile() { PathToSave = @"c:\file13.f", Url = @"http://download.thinkbroadband.com/20MB.zip" });
p.EnqueueFileToDownload(new DownloadFile() { PathToSave = @"c:\file14.f", Url = @"http://download.thinkbroadband.com/20MB.zip" });
p.EnqueueFileToDownload(new DownloadFile() { PathToSave = @"c:\file15.f", Url = @"http://download.thinkbroadband.com/20MB.zip" });
p.EnqueueFileToDownload(new DownloadFile() { PathToSave = @"c:\file16.f", Url = @"http://download.thinkbroadband.com/20MB.zip" });
p.EnqueueFileToDownload(new DownloadFile() { PathToSave = @"c:\file17.f", Url = @"http://download.thinkbroadband.com/20MB.zip" });
p.EnqueueFileToDownload(new DownloadFile() { PathToSave = @"c:\file18.f", Url = @"http://download.thinkbroadband.com/20MB.zip" });
p.EnqueueFileToDownload(new DownloadFile() { PathToSave = @"c:\file19.f", Url = @"http://download.thinkbroadband.com/20MB.zip" });

关于c# - 并行下载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6548155/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com