I am currently using FileHelpers library to extract data from a fixed length size file using MultiRecordEngine the same way like in documentation:
var engine = new MultiRecordEngine(typeof (Orders),
typeof (Customer),
typeof (SampleType));
engine.RecordSelector = new RecordTypeSelector(CustomSelector);
var res = engine.ReadFile("Input.txt");
As per documentation FileHelperAsyncEngine allows async reading of files row by row. However, this is only for one specified type of T.
var engine = new FileHelperAsyncEngine<Customer>();
// Read
using(engine.BeginReadFile("Input.txt"))
{
// The engine is IEnumerable
foreach(Customer cust in engine)
{
// your code here
Console.WriteLine(cust.Name);
}
}
I was unable to find any example of async implementation of MultiRecordEngine, not sure if thats even possible? Any suggestion or should I just leave it as synchronous call engine.ReadFile()?
The MultiRecordEngine
has methods for supporting async usage. See the docs here
There are a couple of examples from the test library of the source code.
[Test]
public void MultpleRecordsFileAsync()
{
engine = new MultiRecordEngine(new RecordTypeSelector(CustomSelector),
typeof (OrdersVerticalBar),
typeof (CustomersSemiColon),
typeof (SampleType));
var res = new ArrayList();
engine.BeginReadFile(FileTest.Good.MultiRecord1.Path);
foreach (var o in engine)
res.Add(o);
Assert.AreEqual(12, res.Count);
Assert.AreEqual(12, engine.TotalRecords);
Assert.AreEqual(typeof (OrdersVerticalBar), res[0].GetType());
Assert.AreEqual(typeof (OrdersVerticalBar), res[1].GetType());
Assert.AreEqual(typeof (CustomersSemiColon), res[2].GetType());
Assert.AreEqual(typeof (SampleType), res[5].GetType());
}
[Test]
public void MultpleRecordsWriteAsync()
{
engine = new MultiRecordEngine(new RecordTypeSelector(CustomSelector),
typeof (OrdersVerticalBar),
typeof (CustomersSemiColon),
typeof (SampleType));
object[] records = engine.ReadFile(FileTest.Good.MultiRecord1.Path);
engine.BeginWriteFile("tempoMulti.txt");
foreach (var o in records)
engine.WriteNext(o);
engine.Close();
File.Delete("tempoMulti.txt");
object[] res = engine.ReadFile(FileTest.Good.MultiRecord1.Path);
Assert.AreEqual(12, res.Length);
Assert.AreEqual(12, engine.TotalRecords);
Assert.AreEqual(typeof (OrdersVerticalBar), res[0].GetType());
Assert.AreEqual(typeof (OrdersVerticalBar), res[1].GetType());
Assert.AreEqual(typeof (CustomersSemiColon), res[2].GetType());
Assert.AreEqual(typeof (SampleType), res[5].GetType());
}