public void routeParallelGeneration(List<long> route_pending)
{
var parallelOptions = new ParallelOptions
{
MaxDegreeOfParallelism = Environment.ProcessorCount
};
var partitions =
Partitioner
.Create(
fromInclusive: 0,
toExclusive: route_pending.Count,
rangeSize: 100
)
.GetDynamicPartitions();
Parallel.ForEach(
source: partitions,
parallelOptions: parallelOptions,
localInit: () =>
{
var conn = new NpgsqlConnection(strConnection);
conn.Open();
var cmd = new NpgsqlCommand();
cmd.Connection = conn;
cmd.CommandText = "SELECT avl_db.process_route(@route_source)";
cmd.CommandType = CommandType.Text;
NpgsqlParameter p = new NpgsqlParameter("@route_source", NpgsqlDbType.Array | NpgsqlDbType.Bigint);
cmd.Parameters.Add(p);
return new { Connection = conn, Command = cmd };
},
body: (source, state, local) =>
{
local.Command.Parameters["@route_source"].Value = route_pending.GetRange(
index: source.Item1,
count: source.Item2 - source.Item1 - 1
);
local.Command.ExecuteNonQuery();
return local;
},
localFinally: local =>
{
local.Connection?.Dispose();
local.Command?.Dispose();
}
);
}
Mod Moved Comments To Chat