【发布时间】:2016-05-20 08:20:59
【问题描述】:
我想在计时器滴答声中逐行阅读(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 毫秒)内进行读取、处理和绘图吗?
【问题讨论】:
-
请注意,您在
if (filewl1.ReadLine() != null && filewl2.ReadLine() != null)处跳过了两行。您读取了两行,但没有将它们存储在变量中,因此您只需在此处松开两行.... -
慢的不是定时器。
tick事件无法跟上正在完成的工作。时间到async,也许吧。 -
0,1280 秒(1280 毫秒) ?
-
确实
if语句的计算应该是if ((linewl1 = filewl1.ReadLine()) != null && (linewl2=filewl2.ReadLine()) != null) -
提示:不要使用
DispatcherTimer,在不阻塞 UI 的情况下工作(例如使用System.Timers.Timer)并在最后调用对 UI 控件的调用。瓶颈是访问文件,所以在这方面尝试concentrate。
标签: c# plot timer filereader