在反应式扩展中包装文件观察器 [英] Wrap a file watcher in reactive extensions

查看:26
本文介绍了在反应式扩展中包装文件观察器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在考虑将文件观察器包装在一个 observable 中以帮助处理事件,但我在弄清楚如何从中获得我想要的行为时遇到了一些麻烦.文件观察器监视放置文件的目录.当文件首次放入该目录时,会在文件观察器上触发 Created 事件.但是,如果文件很大或网络连接很慢,则在文件更新时会触发一系列 Changed 事件.我不想在文件写入完成之前对其进行处理,所以我真正需要的是这个时间线

I've been looking at wrapping a file watcher in an observable to aide in processing events but I'm having some trouble figuring out how to get the behaviour I want out of it. The file watcher watches a directory into which files are placed. When a file is first placed into that directory the Created event is fired on the file watcher. However if the file is large or the network connection slow then a series of Changed events are fired as the file updates. I don't want to process the file until it has finished being written so what I really need is this timeline

|Created    |Changed   |Changed   |Changed                      
________________________________________________ 
^Write starts       ^Write finishes       ^Processing Starts    

我查看了多种过滤 Rx 中事件的方法,但我无法得到我需要的一旦文件文件 X 秒未更改就触发函数".油门不好,因为它会在中间丢失事件.缓冲区不好,因为事件可能发生在缓冲区边界上.

I looked at a number of methods of filtering the events in Rx but I couldn't get what I need which is "fire a function once a file file has not been changed for X seconds". Throttle is no good as it will lose events in the middle. Buffer is no good as events might occur on the buffer boundary.

我曾考虑过使用超时,但我并没有因为它们抛出异常而疯狂,我希望处理在写入文件时开始,而不是在没有更多事件时开始.

I had thought about using timeouts but I wasn't crazy that they threw an exception and I wanted the processing to start as files were being written and not once there were no more events at all.

Reactive Extensions vs FileSystemWatcher 上有一个类似的问题,但从未真正解决过.

There is a similar question at Reactive Extensions vs FileSystemWatcher which was never really solved.

有没有一种方法可以让我轻松做到这一点?我确信这不是一个罕见的用例.

Is there a method such that I can do this easily? I'm sure this is not an uncommon use case.

推荐答案

看看我在 这个答案.

Take a look at my BufferWithInactivity extension method in this answer.

我认为您可以使用它来查找已更改事件中的不活动状态.

I think you could use it to look for inactivity in the changed events.

这篇关于在反应式扩展中包装文件观察器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆