基本上我遇到一个问题,即应用程序在循环3286090或批处理1735之后停止。
我在应用程序的此时列出了1894个经过验证的地址,现在可以进行所有可能的组合并计算每批的距离和行程时间。此子函数调用本地Web服务,每个批处理需要60到180秒才能完成,并将结果写入.csv文件。 (将此文件写入带有现有excel库的excel文件会过度调整内存,因此无法选择。)
没有例外。没有系统日志。并且启用了ctrl + alt + e的每个“break on exception”选项。
if (startNumber <= batchnumber)
{
calculateRouteInfo(waypointDescArrayList, batchnumber, address);
}
代码似乎在这里失败了。 batchnumber到达1735并将其与startNumber进行比较(在这种情况下,我尝试输入1734以重做最后一批/ 1735以执行当前批次并尝试在1736或更高时跳过它。)
无论1736以上的数字,应用程序只是在特定数量的X与batchnumber 1735进行比较时到达终点,即使我只是告诉应用程序将它与1800这样的更高数字进行比较。它就在那里结束。
我试图摆弄条件并检查内存泄漏但事实并非如此。此服务也可以在该号码之前的任何其他批次上正常运行。手动制作该批次并将其发送到网络服务也正常。
这是完整的代码。
private static void preprocessCalculation(xLocate.AddressResponse[] foundAddressess)
{
int batchnumber = 1;
List<xRoute.WaypointDesc[]> waypointDescArrayList = new List<xRoute.WaypointDesc[]>();
foreach (var foundAddress in foundAddressess)
{// 1. foundAddresses containts 1894 foundAddress'
foreach (var address in foundAddress.wrappedResultList)
{// 2. Each foundAddress.wrappedResultlist containts (in this case) 1x address.
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
var watch = Stopwatch.StartNew();
foreach (var foundAddressDest in foundAddressess)
{// 3. Here we combine each address with every address and for each address we make up a batch of combinations that we calculate.
foreach (var addressDest in foundAddressDest.wrappedResultList)
{ // 4.
#region Add waypointDesc
var waypointDescList = new List<xRoute.WaypointDesc>();
waypointDescList.Add(new xRoute.WaypointDesc()
{
linkType = xRoute.LinkType.AUTO_LINKING,
wrappedCoords = new xRoute.Point[] {
new xRoute.Point() {
point = new xRoute.PlainPoint() {
x = address.coordinates.point.x,
y = address.coordinates.point.y
}
}
}
});
waypointDescList.Add(new xRoute.WaypointDesc()
{
linkType = xRoute.LinkType.AUTO_LINKING,
wrappedCoords = new xRoute.Point[] {
new xRoute.Point() {
point = new xRoute.PlainPoint() {
x = addressDest.coordinates.point.x,
y = addressDest.coordinates.point.y
}
}
}
});
waypointDescArrayList.Add(waypointDescList.ToArray());
#endregion
}
}
if (startNumber <= batchnumber)
{ // This calculates the data and does not fail.
calculateRouteInfo(waypointDescArrayList, batchnumber, address);
}
waypointDescArrayList.Clear();
watch.Stop();
elapsedtime += watch.Elapsed.TotalSeconds;
Console.Clear();
Console.WriteLine("Voortgang calculatie... {0}/{1} ({2}s (+{3}s))", batchnumber, totalbatches, elapsedtime, watch.Elapsed.TotalSeconds);
batchnumber++;
}
}
} // 5. When going through the 1735th iteration it skips to the end of this function.
private static void calculateRouteInfo(List<xRoute.WaypointDesc[]> finalList, int batchnumber, xLocate.ResultAddress address)
{
string startlocation = string.Format("{0}-{1}-{2}-{3}", address.country, address.postCode, address.city, address.street);
var matrixDistance = matrixTemplate.Copy();
matrixDistance.Rows.Add(startlocation);
var matrixTime = matrixTemplate.Copy();
matrixTime.Rows.Add(startlocation);
var bulkRouteInfo = xRouteClient.calculateBulkRouteInfo(finalList.ToArray(), null, null, null);
finalList.Clear();
var column = 1;
foreach (var RouteInfo in bulkRouteInfo.wrappedBulkRouteInfoResult)
{
matrixDistance.Rows[0][column] = RouteInfo.routeInfo.distance;
matrixTime.Rows[0][column] = RouteInfo.routeInfo.time;
column++;
}
writeOutputMatrix(fileName, batchnumber, matrixDistance, matrixTime);
}
private static void writeOutputMatrix(string fileName, int batchnumber, DataTable matrixDistance, DataTable matrixTime)
{
string newPath = string.Format("C:/result/{0}/", fileName);
if (!Directory.Exists(newPath))
{
var newDirectory = Directory.CreateDirectory(newPath);
Console.WriteLine("Result mappen aangemaakt.");
}
var matrixDistanceBytes = Encoding.GetEncoding("iso-8859-1").GetBytes(matrixDistance.ToCSV());
using (Stream s = File.Create(string.Format("{0}{1}-distance_{2}.csv", newPath, fileName, batchnumber), matrixDistanceBytes.Length))
{
s.Write(matrixDistanceBytes, 0, matrixDistanceBytes.Length);
Console.WriteLine(string.Format("Result {0}{1}-distance_{2}.csv is aangemaakt!", newPath, fileName, batchnumber));
}
var matrixTimeBytes = Encoding.GetEncoding("iso-8859-1").GetBytes(matrixTime.ToCSV());
using (Stream s = File.Create(string.Format("{0}{1}-time_{2}.csv", newPath, fileName, batchnumber), matrixTimeBytes.Length))
{
s.Write(matrixTimeBytes, 0, matrixTimeBytes.Length);
Console.WriteLine(string.Format("Result {0}{1}-time_{2}.csv is aangemaakt!", newPath, fileName, batchnumber));
}
}
编辑:这是我的解决方法。
private static void preprocessCalculation(xLocate.AddressResponse[] foundAddressess)
{
int batchnumber = 1;
List<xRoute.WaypointDesc[]> waypointDescArrayList = new List<xRoute.WaypointDesc[]>();
foreach (var foundAddress in foundAddressess)
{
if (startNumber < batchnumber)
{
foreach (var address in foundAddress.wrappedResultList)
{
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
var watch = Stopwatch.StartNew();
foreach (var foundAddressDest in foundAddressess)
{
foreach (var addressDest in foundAddressDest.wrappedResultList)
{
#region Add waypointDesc
var waypointDescList = new List<xRoute.WaypointDesc>();
waypointDescList.Add(new xRoute.WaypointDesc()
{
linkType = xRoute.LinkType.AUTO_LINKING,
wrappedCoords = new xRoute.Point[] {
new xRoute.Point() {
point = new xRoute.PlainPoint() {
x = address.coordinates.point.x,
y = address.coordinates.point.y
}
}
}
});
waypointDescList.Add(new xRoute.WaypointDesc()
{
linkType = xRoute.LinkType.AUTO_LINKING,
wrappedCoords = new xRoute.Point[] {
new xRoute.Point() {
point = new xRoute.PlainPoint() {
x = addressDest.coordinates.point.x,
y = addressDest.coordinates.point.y
}
}
}
});
waypointDescArrayList.Add(waypointDescList.ToArray());
#endregion
}
}
calculateRouteInfo(waypointDescArrayList, batchnumber, address);
waypointDescArrayList.Clear();
watch.Stop();
elapsedtime += watch.Elapsed.TotalSeconds;
Console.Clear();
Console.WriteLine("Voortgang calculatie... {0}/{1} ({2}s (+{3}s))", batchnumber, totalbatches, elapsedtime, watch.Elapsed.TotalSeconds);
batchnumber++;
}
}
else
{
batchnumber++;//debug
}
}
}
我把条件提高了一层,到目前为止似乎解决了这个问题。但我仍然想解决这个问题,为什么它停止在中期。 foundAddress.wrappedResultList可以包含多个结果(在此压力测试中它没有。)。
答案 0 :(得分:0)
要暂时解决问题,我已经优化了代码,以防止过多和不必要的foreach循环。
然后保持计数直到崩溃发生的位置,然后从你离开的地方开始一个新的循环。
一个肮脏的解决方法,但它可以工作,而我还不知道另一个解决方案。