【问题标题】:Use FileHelperAsyncEngine crashed with no exception使用 FileHelperAsyncEngine 无异常崩溃
【发布时间】:2017-06-11 13:46:39
【问题描述】:

我正在使用 FileHelpers Engine 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.Class: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;
}

【问题讨论】:

    标签: c# csv exception crash filehelpers


    【解决方案1】:

    对于您的简单 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];
    
                    }
                }
            }
    
    
    
        }
    
    }
    

    【讨论】:

    • 如果我读取内存中的所有数据,它可能太大了。因为会有多个csv文件,这些文件的总和可能大于2GB甚至10GB......
    • 然后简单地创建 Mike2DDyamicData 类的多个实例。如何从单个文件中同时读取。您对 FileHelpersEngine 有同样的问题。你甚至不知道那个图书馆实际上在做什么。至少用我的代码你可以正确处理线程。
    猜你喜欢
    • 2022-10-01
    • 1970-01-01
    • 2019-05-03
    • 2013-08-15
    • 1970-01-01
    • 2014-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多