我在处理ActionBlock中的异步lambda时遇到问题。
var state = new ConcurrentBag<UsersData>();
var getData = new ActionBlock<IEnumerable<int>>(async (userIdsBatch) =>
{
var query = _queryBuilder.GetQuery(userIdsBatch);
string response = await _handler.GetResponseAsync(query).ConfigureAwait(false);
UsersData userData = new Parser().GetUserData(response);
state.Add(userData);
},
new ExecutionDataflowBlockOptions
{
MaxDegreeOfParallelism = _MAX_DEGREE_OF_PARALLELISM // 1
});
foreach (IEnumerable<int> userIdsBatch in GetUserBatches(userIds, 10))
{
getData.Post(userIdsBatch);
}
getData.Complete();
await getData.Completion.ConfigureAwait(false);
// merge the states for different batches synchronously.
ActionBlock将在其中完成异步调用之前退出。产生的任务很少被扔掉,并不是全部都完成了。
是否可以在同步合并结果之前等待所有任务完成?