Created
June 28, 2012 18:01
-
-
Save andersonimes/3012923 to your computer and use it in GitHub Desktop.
[Rx][IO]File System notification throttling via Reactive Extensions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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