小编Bor*_*ign的帖子

Parallel invocation of elements of an IEnumerable

I have an IEnumerable<IEnumerable<T>> method called Batch that works like

var list = new List<int>() { 1, 2, 4, 8, 10, -4, 3 }; 
var batches = list.Batch(2); 
foreach(var batch in batches)
    Console.WriteLine(string.Join(",", batch));
Run Code Online (Sandbox Code Playgroud)

-->

1,2
4,8
10,-4
3
Run Code Online (Sandbox Code Playgroud)

The problem I've having is that I'm to optimize something like

foreach(var batch in batches)
    ExecuteBatch(batch);
Run Code Online (Sandbox Code Playgroud)

by

Task[] tasks = batches.Select(batch => Task.Factory.StartNew(() => ExecuteBatch(batch))).ToArray();
Task.WaitAll(tasks);
Run Code Online (Sandbox Code Playgroud)

or

Action[] executions = batches.Select(batch => new Action(() => ExecuteBatch(batch))).ToArray();
var options = new ParallelOptions …
Run Code Online (Sandbox Code Playgroud)

.net c# linq parallel-processing asynchronous

5
推荐指数
1
解决办法
290
查看次数

标签 统计

.net ×1

asynchronous ×1

c# ×1

linq ×1

parallel-processing ×1