在(三)的时候已经说到模块集合用ForEachAsync的扩展方法分配多个任务,把每个modules的ManifestInfo分析出来的功能加入ConcurrentDictionary。我们先看看这个扩展方法:
public static class EnumerableExtensions { public static Task ForEachAsync<T>(this IEnumerable<T> source, Func<T, Task> body) { var partitionCount = System.Environment.ProcessorCount; return Task.WhenAll( from partition in Partitioner.Create(source).GetPartitions(partitionCount) select Task.Run(async delegate { using (partition) { while (partition.MoveNext()) { await body(partition.Current); } } })); } }