With Reactive Extensions going open-source, as mentioned in Scott Hanselman’s Reactive Extensions (Rx) is now Open Source post, I took a swing at the library, as it solved one of my requirements perfectly. Having the ability to buffer events (or subjects), and process them in chunks, while keeping a time-limit on the delay is extremely simple:
static void Main()
{
// Define a stream of objects
var items = new Subject();
items
// Buffer the stream into chunks of 5, yet process whatever you get after a maximum of 10 seconds
.Buffer(TimeSpan.FromSeconds(10), 5)
// Process the buffered items
.Subscribe(buffer =>
{
foreach (var item in buffer)
Console.WriteLine("\tProccessed {0}", item);
});
string line;
while (!string.IsNullOrEmpty(line = Console.ReadLine()))
{
items.OnNext(line);
}
// Notify the stream that there are no more objects, so the remaing buffered objects are processed immediately
items.OnCompleted();
}
The following links proved extremely helpful to get me started quickly: