How to implement a DelayTextBox - Part 2

Apr 16, 2012 by Stefan

Solution #2 - Implementing the DelayTextBox using the IObservable interface from ReactiveExtensions

After seeing Part 1, now I will implement a DelayTextBox class (we’ll call it DelayTextBoxReactive) using the IObservable.Throttle method from the Reactive Extensions framework.

This method ignores values from an observable sequence that are followed by another value before the specified time span.

      public DelayTextBoxReactive()       
      

      

      public IDisposable Subscribe(Action<string> handler, double delay)       
                
         var textChanged = Observable.FromEvent<EventArgs>(this, "TextChanged");         
        
         textChanged.Throttle(TimeSpan.FromMilliseconds(delay)).ObserveOn(this).
                Subscribe(args => handler(this.Text));

         return textChanged as IDisposable;

Note: The delay variable represents the time to wait for the user’s input (similar to the Delay property in the first approach).

I’m detailing below each part of this code:

var textChanged = Observable.FromEvent< EventArgs >(this, "TextChanged");

Basically, this code creates an IObservable object for the TextChanged event of the TextBox

textChanged.Throttle(TimeSpan.FromMilliseconds(delay));

This specifies that the IObservable must ignore TextChanged events raised before the specified time has elapsed.

ObserveOn(this).Subscribe(args = > handler(this.Text));

After the time has elapsed without any TextChanged event being raised, this code asynchronously notifies the observers by calling the handler (in our case, this is a void method with a string parameter).

At this point, you can use the DelayTextBoxReactive class in your form / user control as follows:

textBoxInput.Subscribe(OnTextChangeCompleted, Constants.DefaultDelayDelta);

Note: in this example, textBoxInput is of type DelayTextBoxReactive.

Conclusion:

As compared to solution #1 (using a Timer), the second approach (using the Rx / Reactive Extensions library) brings quite a few advantages:

        - A major advantage is the fact that memory leaks can be easily prevented, as Rx provides a more intuitive mechanism for event handling disposal (the Subscribe method returns an IDisposable reference).

        - Reactive Extensions simplifies a lot of aspects of asynchronous programming, as it reduces the necessary effort for implementing a solution (for example, in my samples above, the code for the DelayTextBox implementation is significantly larger than the DelayTextBoxReactive implementation).

        - Increased code clarity and LINQ fluent method call syntax.
 


Tags: ASP.NET  Programming 


Comments


cheap ink cartridges commented on 5/19/2012 10:07:04 AM

Thanks for this helping post now i can easily implement delaytextbox.
     

Virus Removal Services commented on 5/21/2012 12:42:39 PM

It seems to be a nice information. Thanks for sharing code. I will try for this.
     

Stefan commented on 5/21/2012 2:49:22 PM

Re: Virus Removal Services
Re: cheap ink cartridges

Thank you for stopping by.
     

iphone applications development commented on 6/6/2012 6:23:57 AM

Stefan, thank you for this post. I tried implementing DelayTextBox myself with the help of your code and it worked really nicely.
     

Stefan commented on 6/6/2012 1:14:52 PM

Glad to hear this, and thanks for stopping by.

p.s. I've just visited your site and you've done some really interesting projects. See you on your blog!
     

Magnetic Messaging commented on 8/4/2015 7:45:13 AM

find a lot of approaches after visiting your post. I was exactly searching for. Thanks for such post and please keep it up.
     

Venus Factor Reviews commented on 8/4/2015 7:46:21 AM

Your style is so unique compared to other people I have read stuff from. Thank you for posting when you have the opportunity, Guess I'll just bookmark this blog...
     

Your Comment:







Blog Home   SBP Home
RSS Feed       Contact








 Blog Archives  |  Terms of Use  |  Privacy Policy
© 2017 SBP Romania. All rights reserved.