Chapter143:ReactiveExtensions(Rx)

No Comments

Section143.1:ObservingTextChangedeventonaTextBox

AnobservableiscreatedfromtheTextChangedeventoftheTextBox.Alsoanyinputisonlyselectedifit’sdifferent from the last input and if there was no input within 0.5 seconds. The output in this example is sent to the console.

Observable

.FromEventPattern(textBoxInput,”TextChanged”)

.Select(s=>((TextBox)s.Sender).Text)

.Throttle(TimeSpan.FromSeconds(0.5))

.DistinctUntilChanged()

.Subscribe(text=>Console.WriteLine(text));

Section143.2:StreamingDatafromDatabasewith Observable

AssumehavingamethodreturningIEnumerable<T>,f.e.

privateIEnumerable<T>GetData()

{

try

{

//returnresultsfromdatabase

}

catch(Exceptionexception)

{

throw;

}

}

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

intbufferSize=200;

Observable

.Start(()=>GetData())

.SelectMany(s=>s)

.Buffer(bufferSize)

.ObserveOn(SynchronizationContext.Current)

.Subscribe(items=>

{

Console.WriteLine(“Loaded{0}elements”,items.Count);

//dosomethingontheUIlikeincrementingaProgressBar

},

()=>Console.WriteLine(“Completedloading”));

About us and this blog

We are a digital marketing company with a focus on helping our customers achieve great results across several key areas.

Request a free quote

We offer professional SEO services that help websites increase their organic search score drastically in order to compete for the highest rankings even when it comes to highly competitive keywords.

Subscribe to our newsletter!

More from our blog

See all posts
No Comments

Leave a Comment