私は100K +ファイルをダウンロードしており、一度に100ファイルなどのパッチでそれを実行します。 は、次の100をダウンロードし、その後、一度に100個のファイルをダウンロードすることを期待して期待forループでx回ずつ非同期で実行します。
static void Main(string[] args) {
Task.WaitAll(
new Task[]{
RunAsync()
});
}
// each group has 100 attachments.
static async Task RunAsync() {
foreach (var group in groups) {
var tasks = new List<Task>();
foreach (var attachment in group.attachments) {
tasks.Add(DownloadFileAsync(attachment, downloadPath));
}
await Task.WhenAll(tasks);
}
}
static async Task DownloadFileAsync(Attachment attachment, string path) {
using (var client = new HttpClient()) {
using (var fileStream = File.Create(path + attachment.FileName)) {
var downloadedFileStream = await client.GetStreamAsync(attachment.url);
await downloadedFileStream.CopyToAsync(fileStream);
}
}
}
。
実際の 同時に多くのダウンロードを行います。すぐにエラーが発生するUnable to read data from the transport connection: An existing connection was forcibly closed by the remote host
に例外処理を追加することを忘れないでください。クエンティンが使うものは失敗します。 – Bartosz
私は同意します。重複ではありません。私の推測では、HttpClientメソッドはあなたが望むよりも早く復帰するでしょう。 – BradleyDotNET
[Asynchronous HttpClientは.Net 4.5から集中型負荷アプリケーションに悪い選択ですか?](https://stackoverflow.com/questions/16194054/is-async-httpclient-from-net-4-5-a)大量の負荷を必要とするアプリケーション) –