1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
| static async Task<ObservableCollection<Pt>> ImportPointAsync(int srid, string mdbPath, IProgress<int> progress)
{
DataTable dtPoints = new DataTable();
DataTable dtPCodes = new DataTable();
--- chargement des tables à partir de la mdb ---
if (dtPoints == null || dtPoints.Rows.Count == 0 || dtPCodes == null || dtPCodes.Rows.Count == 0)
return null;
ImmutableList<Pt> points = new List<Pt>(dtPoints.Rows.Count).ToImmutableList();
int totalCount = 0, count = 1, i=0;
Object obj = new Object();
var maListeDeListesDePoints = dtPoints.Rows.OfType<System.Data.DataRow>().GroupBy(x => i++ / 5).Select(x => x.ToList()).ToList(); // Constitue des listes de 5 points
totalCount = maListeDeListesDePoints.Count;
await Task.Run(() =>
Parallel.ForEach(maListeDeListesDePoints, new ParallelOptions { MaxDegreeOfParallelism = Environment.ProcessorCount / 2 }, listeDePoints => {
for (int j=0; j< listeDePoints.Count; j++)
{
DataRow drPoint = listeDePoints[j];
DataRow[] drsPCodes = dtPCodes.Select("IdPoint = " + drPoint["IdPoint"]);
List<int> lstPCodes = new List<int>();
foreach (DataRow drPCode in drsPCodes)
{
int pcode = 0;
if (drPCode != null && int.TryParse(drPCode["PCode"].ToString(), out pcode))
lstPCodes.Add(pcode);
}
Pt newPoint = new Pt(drPoint["nomPoint"].ToString(), (double)drPoint["PointX"], (double)drPoint["PointY"], lstPCodes, srid);
if (newPoint != null)
points.Add(newPoint);
}
if (progress != null)
{
int progressCount;
lock (obj)
{
count++;
progressCount = count;
}
progress.Report((progressCount * 100 / totalCount));
}
})
);
if (points != null && points.Count > 0)
return new ObservableCollection<Pt>(points.Distinct());
return null;
} |