Мне нужно вычислить расстояния между примерно 26 000 компаний и найти медиану всех расстояний. Однако программа выдает следующее исключение:
at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
at System.Threading.Tasks.Task.Wait()
at System.Threading.Tasks.Parallel.ForWorker[TLocal](Int32 fromInclusive, Int32 toExclusive, ParallelOptions parallelOptions, Action`1 body, Action`2 bodyWithState, Func`4 bodyWithLocal, Func`1 localInit, Action`1 localFinally)
at System.Threading.Tasks.Parallel.For(Int32 fromInclusive, Int32 toExclusive, Action`2 body)
at DataHelper.FindMediumBase.CountDistancesPerKilometer()
Это моя программа:
protected void CountDistancesPerKilometer()
{
try
{
int EnterprisesCount = enterprises.Count;
Stopwatch watch = new Stopwatch();
watch.Start();
Parallel.For(0, enterprises.Count, (i, loopStateOut) =>
{
Enterprise eOut = enterprises.ElementAt(i);
for (int j = i + 1; j < enterprises.Count; j++)
{
Enterprise eIn = enterprises.ElementAt(j);
double distance = Math.Sqrt((eOut.Point.X - eIn.Point.X) * (eOut.Point.X - eIn.Point.X) +
(eOut.Point.Y - eIn.Point.Y) * (eOut.Point.Y - eIn.Point.Y)) / 1000;
if (0 == distance)
continue;
else
DistanceFiles[(int)distance].FileRowCount++;
}
});
watch.Stop();
System.Console.WriteLine(watch.ElapsedTicks);
}
catch (Exception ex)
{
Log.WriteError(ex.StackTrace);
}
}
PS:
enterprises: List<Enterprise>
DistanceFiles : ConcurrentDictionary<int, DistanceFile>
DistanceFiles[(int)distance].FileRowCount++;
— Вы уверены, чтоdistance
— существующий ключ в словаре? - person Yuval Itzchakov   schedule 28.03.2016DistanceFiles
? - person Dmitry Bychenko   schedule 28.03.2016ConcurrentDictionary<int, DistanceFile>
- person Yuval Itzchakov   schedule 28.03.2016Parallel.For
изменено наfor(...)
) - person Dmitry Bychenko   schedule 28.03.2016