【问题标题】:How to read from a constantly changing file a line at a timer tick and avoid delays in C#?如何在计时器滴答声中从不断变化的文件中读取一行并避免 C# 中的延迟?
【发布时间】: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 &amp;&amp; filewl2.ReadLine() != null) 处跳过了两行。您读取了两行,但没有将它们存储在变量中,因此您只需在此处松开两行....
  • 慢的不是定时器。 tick 事件无法跟上正在完成的工作。时间到async,也许吧。
  • 0,1280 秒(1280 毫秒) ?
  • 确实if语句的计算应该是if ((linewl1 = filewl1.ReadLine()) != null &amp;&amp; (linewl2=filewl2.ReadLine()) != null)
  • 提示:不要使用DispatcherTimer,在不阻塞 UI 的情况下工作(例如使用System.Timers.Timer)并在最后调用对 UI 控件的调用。瓶颈是访问文件,所以在这方面尝试concentrate

标签: c# plot timer filereader


【解决方案1】:

我按照评论中的建议更改了计时器 (System.Timers.Timer),也提到了这个post

这是我处理计时器的方式:

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 
     }
}

谢谢你!

【讨论】:

    猜你喜欢
    • 2015-05-14
    • 1970-01-01
    • 1970-01-01
    • 2020-09-18
    • 1970-01-01
    • 1970-01-01
    • 2013-10-15
    • 1970-01-01
    • 2020-05-20
    相关资源
    最近更新 更多