我想在计时器滴答声(DispatcherTimer
带有~0,1280秒滴答声)逐行读取。该文件每隔几毫秒更新一次,因为它是由另一个应用程序生成的持续写入文件。我想在~0,1280(128毫秒)处阅读一行。
我假设计时器开始时有 1 秒的延迟,让另一个应用程序用几行填充文本文件。
之后,我读了一行,我也处理了它。每行包含 64 个用空格分隔的数字。从这个数字中,我只需要 20 个放置在不同位置 = index_of interest
.为此,我使用 line.Split()[index_of interest]
返回所需的数字。之后,对于这 20 个数字中的每一个,我应用一个公式来计算增量。
这些数字存储在一个全局变量中,我用它来绘制。
这是代码:
private void dispatcherTimer_Tick(object sender, EventArgs e)
{
//Read a line at a timer tick
if ((linewl1 = filewl1.ReadLine()) != null && (linewl2=filewl2.ReadLine()) != null)
{
//There are 2 files that I need to read at the same time
var channel = 0;
for (var i = 0; i < DataAccessor.TotalNumberOfChannels.Length; i++)
{
//DataAccessor.TotalNumberOfChannels is an array of 0,1 and 1 indicates the index of interest
if (DataAccessor.TotalNumberOfChannels[i] == 1)
{
//I apply the methods that calculates the desired deltas
HemoDynamicData.RealTimeSimulationHbCalculation(
Convert.ToDouble(linewl1.Split()[i]) * Math.Pow(10, -7),
Convert.ToDouble(linewl2.Split()[i]) * Math.Pow(10, -7),
frames,
channel, i, new Coefficient(6.4, 5.75, 3.843707, 1.4865865, 1.798643, 2.526391, 3),
DataAccessor.UsedChannels);
channel++;
}
//Than I plot each of those 20x2 numbers
Plotter.UpdateSimulatioPlot(Max, Min, frames, HemoDynamicData, DataAccessor);
}
frames++;
}
}
计时器非常慢。有没有另一种方法可以让我在 0,1280 秒(128 毫秒)内完成读取、处理和绘图?
我按照评论中的建议更改了计时器(System.Timers.Timer
),并参考了这篇文章。
以下是我处理计时器的方法:
public StreamReader file;
public StreamReader file2;
private string filewl1; //file paths
private string filewl2; //file paths
public void TimerStart()
{
systemTimer.Interval = 128;
systemTimer.Elapsed += systemTimer_Elapsed;
systemTimer.Enabled = true;
FileStream fileStream = File.Open(filewl1, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
file = new StreamReader(fileStream);
FileStream fileStream2 = File.Open(filewl2, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
file2 = new StreamReader(fileStream2);
}
private void systemTimer_Elapsed(Object source, System.Timers.ElapsedEventArgs e)
{
if (!file.EndOfStream)
{
linewl1 = file.ReadLine();
linewl2 = file2.ReadLine();
// ... do something
}
}
谢谢辛纳特!