Reactive Extensions (Rx)

Observing TextChanged event on a TextBox

An observable is created from the TextChanged event of the TextBox. Also any input is only selected if it's different from the last input and if there was no input within 0.5 seconds. The output in this example is sent to the console.

     .FromEventPattern(textBoxInput, "TextChanged")
     .Select(s => ((TextBox) s.Sender).Text)
     .Subscribe(text => Console.WriteLine(text));

Streaming Data from Database with Observable

Assume having a method returning IEnumerable<T>, f.e.

private IEnumerable<T> GetData()
        // return results from database 
    catch(Exception exception)

Creates an Observable and starts a method asynchronously. SelectMany flattens the collection and the subscription is fired every 200 elements through Buffer.

int bufferSize = 200;

    .Start(() => GetData())
    .SelectMany(s => s)
    .Subscribe(items => 
        Console.WriteLine("Loaded {0} elements", items.Count);
        // do something on the UI like incrementing a ProgressBar
    () => Console.WriteLine("Completed loading"));