Note that there are some explanatory texts on larger screens.

plurals
  1. POReactive Extensions vs FileSystemWatcher
    primarykey
    data
    text
    <p>One of the things that has long bugged me about the FileSystemWatcher is the way it fires multiple events for a single logical change to a file. I know why it happens, but I don't want to have to care - I just want to reparse the file once, not 4-6 times in a row. Ideally, there would be an event that only fires when a given file is done changing, rather than every step along the way.</p> <p>Over the years I've come up with various solutions to this problem, of varying degrees of ugliness. I thought Reactive Extensions would be the ultimate solution, but there's something I'm not doing right, and I'm hoping someone can point out my mistake.</p> <p>I have an extension method:</p> <pre><code>public static IObservable&lt;IEvent&lt;FileSystemEventArgs&gt;&gt; GetChanged(this FileSystemWatcher that) { return Observable.FromEvent&lt;FileSystemEventArgs&gt;(that, "Changed"); } </code></pre> <p>Ultimately, I would like to get one event per filename, within a given time period - so that four events in a row with a single filename are reduced to one event, but I don't lose anything if multiple files are modified at the same time. <code>BufferWithTime</code> sounds like the ideal solution.</p> <pre><code>var bufferedChange = watcher.GetChanged() .Select(e =&gt; e.EventArgs.FullPath) .BufferWithTime(TimeSpan.FromSeconds(1)) .Where(e =&gt; e.Count &gt; 0) .Select(e =&gt; e.Distinct()); </code></pre> <p>When I subscribe to this observable, a single change to a monitored file triggers my subscription method four times in a row, which rather defeats the purpose. If I remove the <code>Distinct()</code> call, I see that each of the four calls contains two identical events - so there is some buffering going on. Increasing the TimeSpan passed to <code>BufferWithTime</code> seems to have no effect - I went as high as 20 seconds without any change in behavior.</p> <p>This is my first foray into Rx, so I'm probably missing something obvious. Am I doing it wrong? Is there a better approach? Thanks for any suggestions...</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload