Multiple async/await chaining

前端 未结 3 2092
离开以前
离开以前 2021-01-06 22:50

How to make multiple async/await chaining in C#? For example, start few HTTP requests and then do not wait all of them, but start new request after each completed?<

3条回答
  •  南笙
    南笙 (楼主)
    2021-01-06 23:29

    If you want to perform some async action on a collection of items with a limited degree of parallelism, then probably the simplest way is to use ActionBlock from TPL Dataflow, since it supports async delegates (unlike most other parallel constructs in TPL, like Parallel.ForEach()).

    var block = new ActionBlock(
        url => DownloadAsync(url),
        new ExecutionDataflowBlockOptions { MaxDegreeOfParallelism = few });
    
    foreach (var url in urls)
        block.Post(url);
    
    block.Complete();
    await block.Completion;
    

提交回复
热议问题