1

I have a binary data file that is written to from a live data stream, so it keeps on growing as stream comes. In the meanwhile, I need to open it at the same time in read-only mode to display data on my application (time series chart).

Opening the whole file takes a few minutes as it is pretty large (a few 100' MBytes).
What I would like to do is, rather than re-opening/reading the whole file every x seconds, read only the last data that was added to the file and append it to the data that was already read.

4

3 回答 3

3

我建议使用FileSystemWatcher来通知文件的更改。从那里缓存事件之间的文件大小等信息,并添加一些逻辑以仅响应整行等。您可以使用 FileStream 类的 Seek() 方法跳转到文件中的特定点并读取只有从那里。我希望它有所帮助。

于 2012-07-05T15:16:57.533 回答
0

如果您控制此文件的写入,我会将其拆分为多个预定义大小的文件。

当作者确定当前文件大于 50MB 时,关闭它并立即创建一个新文件来写入数据。写入此数据的进程应该始终知道要将接收到的数据写入的当前文件。

读取器线程/进程将按顺序读取所有这些文件,当当前文件被完全读取时跳转到下一个文件。

于 2012-07-05T15:15:36.820 回答
0

您可能可以使用 FileSystemWatcher 来监视文件中的更改,例如此处给出的示例:Reading changes in a file in real-time using .NET

但我建议您评估另一种解决方案,包括队列,如 RabbitMQ 或 Redis - 任何具有订阅者-发布者模型的队列。然后,您只需将实时数据推送到队列中,并且将有 2 个不同的侦听器(订阅者) - 一个用于保存在文件中,另一个用于处理最后附加的数据。通过这种方式,您可以更灵活地分配应用程序的负载。

于 2012-07-05T15:18:55.537 回答