How to do it...

  1. If you haven't already done so, create a new Windows form application and call it winformRx. Open the form designer and in Toolbox, search for the TextBox control and add it to your form.
  1. Next, add a label control to your form.
  1. Double-click on your Windows form designer to create the onload event handler. Inside this handler, add some code to read the text entered into the textbox and only display that text 5 seconds after the user has stopped typing. This is achieved using the Throttle keyword. Add a subscription to the searchTerm variable, writing the result of the text input to the label control's text property:
        private void Form1_Load(object sender, EventArgs e) 
{
var searchTerm = Observable.FromEventPattern<EventArgs>(
textBox1, "TextChanged").Select(x => ((TextBox)x.Sender)
.Text).Throttle(TimeSpan.FromMilliseconds(5000));

searchTerm.Subscribe(trm => label1.Text = trm);
}
Note that you might need to add System.Reactive.Linq in your using statements.
  1. Run your application and start typing some text into the textbox. Immediately, we will receive an exception. It is a cross-thread violation. This occurs when there is an attempt to update the UI from a background thread. The Observable interface is running a timer from System.Threading, which isn't on the same thread as the UI. Luckily, there is an easy way to overcome this. Well, it turns out that the UI-threading capabilities lie in a different assembly, which we find easiest to get via the Package Manager Console:
  1. Navigate to View | Other Windows | Package Manager Console to access the Package Manager Console.
  1. Enter the following command:
      PM> Install-Package System.Reactive.Windows.Forms

This will add the System.Reactive.Windows.Forms.3.1.1 to your winformRx project. You should, therefore, see the following in the output: Successfully installed 'System.Reactive.Windows.Forms 3.1.1' to winformRx

Note that you need to ensure that the Default project selection is set to winformRx in the Package Manager Console. If you don't see this option, resize the Package Manager Console screen width ways until the option is displayed. This way you can be certain that the package is added to the correct project.
  1. After the installation completes, modify your code in the onload event handler and change searchTerm.Subscribe(trm => label1.Text = trm);, which does the subscription, to look like this:
        searchTerm.ObserveOn(new ControlScheduler(this)).Subscribe(trm => label1.Text = trm);

You will notice that we are using the ObserveOn method here. What this basically tells the compiler is that the this keyword in new ControlScheduler(this) is actually a reference to our Windows form. Therefore, ControlScheduler will use the Windows forms timers to create the interval to update our UI. The message happens on the correct thread, and we no longer have our cross-thread violation.

  1. If you have not added the System.Reactive.Concurrency namespace to your project, Visual Studio will underline the ControlScheduler line of code with a squiggly line. Pressing Ctrl + . (period) will allow you to add the missing namespace.
  1. This means that System.Reactive.Concurrency contains a scheduler that can talk to Windows forms controls so that it can do the scheduling. Run your application again and start typing some text into your textbox:
  1. Around 5 seconds after we stop typing, the throttle condition is fulfilled and the text is output to our label:
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.202.27