Skip to content

Instantly share code, notes, and snippets.

@andersonimes
Created June 28, 2012 18:01
Show Gist options
  • Save andersonimes/3012923 to your computer and use it in GitHub Desktop.
Save andersonimes/3012923 to your computer and use it in GitHub Desktop.
[Rx][IO]File System notification throttling via Reactive Extensions
using System;
using System.Collections.Generic;
using System.Linq;
using System.Dynamic;
using System.Reflection;
using System.Reactive.Linq;
using Rxx;
using System.IO;
namespace ConsoleApplication17
{
class Program
{
static void Main(string[] args)
{
FileSystemWatcher fsw = new FileSystemWatcher(@"c:\temp\watchme");
//Watch extension method provided by Rxx library. Otherwise
//we would call "FromEventPattern" 5 times.
var notificationFireHose = fsw.Watch(WatcherChangeTypes.All);
//Group each notification by their path and then throttle each
//filepath for 200 ms.
var filteredNotifications = notificationFireHose
.GroupBy(n => n.FullPath)
.Select(g => g.Throttle(TimeSpan.FromMilliseconds(200)))
.Merge();
filteredNotifications.Subscribe(f => Console.WriteLine("File done: " + f.FullPath));
Console.Read();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment