c# - 使用FileHelperAsyncEngine崩溃无一异常(exception)

标签 c# csv exception crash filehelpers

我正在使用FileHelpers引擎FileHelperAsyncEngine<T>。读取具有超过500,000行的大型CSV文件,我需要提取一些字段以填充SHP文件(ArcGIS)。

但是,当我使用BeginReadFile并尝试选择一些数据时,即使我已完成获取全局异常的操作,应用程序也不会发生任何异常崩溃。但是没有任何异常实现,我在读取线程的地方打印了CSV行。

程序崩溃时,日志文件中的最后一行每次都不同。

这是我的代码:

1.使用FileHelpersEngine的方法

public Dictionary<int, double> FetchByStepIndex(int stepindex)
{
    try
    {
        using (var engine = new FileHelperAsyncEngine<Mike2DDynamicData>())
        {
            using (engine.BeginReadFile(CsvPath))
            {
                var temp=new Dictionary<int, double>();
                foreach (var itemData in engine)
                {
                    if (itemData.StepIndex != stepindex) continue;
                    temp.Add(itemData.ElementID,double.Parse(itemData.TotalWaterDepth));
                    LogHelper.WriteLog(
                        itemData.StepIndex + "_" + itemData.ElementID + "_" + itemData.TotalWaterDepth,
                        LogMessageType.Info);
                }
                /* The codes when not debugging like ↓
                var temp = engine.Where(w => w.StepIndex == stepindex)
                    .Select(s => new { s.ElementID, s.TotalWaterDepth })
                    .ToDictionary(d => d.ElementID, d => d.TotalWaterDepth);
                    */
                engine.Close();
                return temp;
            }
        }
    }
    catch (FileHelpersException e)
    {
        throw e;
    }
}

2.分类:Mike2DDynamicData:
[DelimitedRecord(",")]
[IgnoreFirst]
public class Mike2DDynamicData
{
    public int StepIndex;

    [FieldNullValue(typeof(DateTime),"2017-1-1 00:00:00")]
    [FieldConverter(ConverterKind.Date,"yyyy-MM-dd HH:mm:ss")]
    public DateTime Time;
    public int ElementID;

    [FieldValueDiscarded]
    public string SurfaceElevation;

    public string TotalWaterDepth;

    [FieldValueDiscarded] public string CurrentSpeed;
}

最佳答案

对于您简单的csv文件,使用第三部分dll是不值得的麻烦。只需自己编写代码。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.csv";
        static void Main(string[] args)
        {
            new Mike2DDynamicData(FILENAME);
        }
    }
    public class Mike2DDynamicData
    {
        public static List<Mike2DDynamicData> data = new List<Mike2DDynamicData>();


        public int StepIndex;

        public DateTime Time;
        public int ElementID;

        public string SurfaceElevation;

        public string TotalWaterDepth;

        public string CurrentSpeed;

        public Mike2DDynamicData() { }
        public Mike2DDynamicData(string filename)
        {

            StreamReader reader = new StreamReader(filename);

            string inputline = "";
            int lineNumber = 0;
            while((inputline = reader.ReadLine()) != null)
            {
                if (++lineNumber > 1)
                {
                    string[] splitArray = inputline.Split(new char[] { ',' });

                    Mike2DDynamicData newRow = new Mike2DDynamicData();
                    data.Add(newRow);

                    newRow.StepIndex = int.Parse(splitArray[0]);
                    newRow.Time = DateTime.Parse(splitArray[1]);
                    newRow.ElementID = int.Parse(splitArray[2]);
                    newRow.SurfaceElevation = splitArray[3];
                    newRow.TotalWaterDepth = splitArray[4];
                    newRow.CurrentSpeed = splitArray[5];

                }
            }
        }



    }

}

关于c# - 使用FileHelperAsyncEngine崩溃无一异常(exception),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44484562/

相关文章:

java - Spring Boot ConstraintViolationException 适用于创建但不适用于更新

C#文件夹同步库

javascript - Highcharts (Highstock) : Get data from external CSV, 无法正确显示日期

c# - ASP.NET 使用 MVC 将 linq 查询结果绑定(bind)到 HTML.DropDownList()

csv - gnuplot 中 CSV 数据的双倍刻度

c# - GUI 从 CSV 读取

java - 从 UNKNOWN 到 UNKNOWN 的转换是不支持的异常(在用于迁移的 Java 程序中)

exception - JUnit : a Successful test if an exception is catched

c# - Xamarin.forms 中的 UITests 发布失败

c# - 您如何在 WPF 中仅检查 Visual Studio 2010 设计器(以便不针对 Blend 的设计器)?