I'm using this example to create partitions and is working perfectly in my VS2015 app. I tried to move the code to VS2013, using 4.5.2 framework, and now have an error on this line of code:
VS2013 12.0.40629.00 Update 5
body: (source, state, local) =>
says
Error 5 Delegate 'System.Func<System.Tuple<long,long>,System.Threading.Tasks.ParallelLoopState,long,AnonymousType#1,AnonymousType#1>' does not take 3 arguments
I found this question: Whats wrong in this Parallel.For Code? which is very similar. But it does not appear to actually be the same problem.
The suggested answer doesn't have any syntax error in 2013 but doesn't use partitions, so I'm not sure how adapt my code to that one. And the recommendation is to add three parameters and mine already has three parameters.
This is a reduced version of my code:
public void NearLinkParallelGeneration(avl_range avl_pending)
{
var parallelOptions = new ParallelOptions
{
MaxDegreeOfParallelism = Environment.ProcessorCount + 2
};
var partitions = Partitioner
.Create(
fromInclusive: avl_pending.begin,
toExclusive: avl_pending.end,
rangeSize: 100
)
.GetDynamicPartitions();
Parallel.ForEach(
source: partitions,
parallelOptions: parallelOptions,
localInit: () =>
{
NpgsqlConnection conn = new NpgsqlConnection(strConnection);
NpgsqlCommand cmd = new NpgsqlCommand();
conn.Open();
return new { Connection = conn, Command = cmd };
},
body: (source, state, local) => -- HERE IS THE ERROR
{
return local;
},
localFinally: local =>
{
local.Connection?.Dispose();
local.Command?.Dispose();
}
);