C#:如何停止循环csv文件中的数据并存储上次读取的值
我有一个windows窗体应用程序,它拾取一个csv文件并循环遍历其中的客户ID。对每个客户ID进行更新 我有两个问题:C#:如何停止循环csv文件中的数据并存储上次读取的值,c#,C#,我有一个windows窗体应用程序,它拾取一个csv文件并循环遍历其中的客户ID。对每个客户ID进行更新 我有两个问题: 我有一个暂停按钮,它应该停止处理csv文件中的customerID,并存储最后一个进程customerID。如何停止在csv中查看customerID并存储最后处理的customerID 下次启动应用程序时,它应该在上次处理的客户ID之后开始处理 请问你能给我一些建议吗 //initially the customerID will be 0 int customerID =
//initially the customerID will be 0
int customerID = 0;
string inputFilePath = "C:\\customer_refno\\customerids.csv";
using (StreamReader input = File.OpenText(inputFilePath))
using (CsvReader csvread = new CsvReader(input))
{
IEnumerable<dynamic> records = csvread.GetRecords<dynamic>();
//StringBuilder buildtext = new StringBuilder();
foreach (var record in records)
{
//process customerID
lastProcessed = record.CustomerID;
}
}
private void btpause_click(object sender, EventArgs e)
{
//complete processing the currently processing customerID and stop
//store last processed customer ID
}
//最初customerID为0
int customerID=0;
string inputFilePath=“C:\\customer\u refno\\customerids.csv”;
使用(StreamReader输入=File.OpenText(inputFilePath))
使用(CsvReader csvread=新CsvReader(输入))
{
IEnumerable records=csvread.GetRecords();
//StringBuilder buildtext=新建StringBuilder();
foreach(记录中的var记录)
{
//处理客户ID
lastProcessed=record.CustomerID;
}
}
私有void btpause\u单击(对象发送方,事件参数e)
{
//完成当前正在处理的customerID的处理并停止
//存储上次处理的客户ID
}
您可以通过存储下划线流的位置来实现。但要注意缓冲区,可能是同一个文件块中的一些文件需要跳过
以下是一个示例:
private static long _lastPos = 0;//Store this in a file and load on Apllication start
private static int _lastProcessed = -1;//Store this in a file and load on Apllication start
private static int _bufferSize = 4096;
private static bool _break = true; //Set or reset in btpause_click
static void Main(string[] args)
{
Loop();
Loop();
Loop();
}
private static void Loop()
{
int customerID = 0;
string inputFilePath = "D:\\temp\\customerids.csv";
using (StreamReader input = new StreamReader(inputFilePath, Encoding.UTF8, true, _bufferSize))
{
if (_lastPos > 0) input.BaseStream.Position = _lastPos;
using (CsvReader csvread = new CsvReader(input, new Configuration(){Delimiter = "\t"}))
{
IEnumerable<dynamic> records = csvread.GetRecords<dynamic>();
//StringBuilder buildtext = new StringBuilder();
foreach (var record in records)
{
//process customerID
var cosId = Int32.Parse(record.CustomerID);
if (_lastProcessed >= cosId) continue; //Skip to next if lower than processed
_lastProcessed = cosId;
_lastPos = input.BaseStream.Position - _bufferSize;
Console.WriteLine(" > " + record.CustomerID + "\t" + record.Name);
if (_break) {
//Do staff needed at the break, like save variables
break;
}
}
}
}
}
CsvReader不是.Net Framework的一部分,您从哪里使用它?是的,您是对的,很抱歉没有粘贴引用。我使用的是csvHelper Library,您可以使循环异步,也可以在需要时使用后台工作程序和中断。创建一个变量isStop=false,并检查循环中是否存在isStop。在click事件处理程序上,更改isStop=true。你的循环应该是异步的,或者在后台独立于显示器,以便在循环工作时能够单击按钮并运行你想要的。谢谢Daniel,这给了我一个实现的想法。但是你能给我一些建议吗,关于如何阻止streamreader在点击按钮时阅读?我无法在点击按钮时停止它。没有关闭方法查看我的编辑,您可以简单地将_break设置为true,如果需要,您可以在检查_break的循环中输入一些操作。
private void btpause_click(object sender, EventArgs e)
{
_break = true;
}
// ...
private static void Loop()
//... Modify loop, also see first sample in the case break is set:
Console.WriteLine(" > " + record.CustomerID + "\t" + record.Name);
if (_break) {
//Do staff needed at the break, like save variables
break;
}
//....