Archive

Posts Tagged ‘Rx’

Observable Kinect

December 7th, 2011 2 comments

When the Kinect SDK was first introduced I said that it should really be used in conjunction with Rx to really unlock the power of the Kinect hardware. I started making my first application for the Kinect a few days ago and it turns out that my impression of the SDK that I saw fly by on stage during a Mix Keynote was correct, here was a piece of code that could really use a prescription of Rx! Take two IObservable<T>s and call me in the morning.

I checked around NuGet and looked for some open source Rx for Kinect implementations but there didn’t seem to be any so I created one. There are two parts, a wrapper for the standard Kinect APIs and a separate one for the speech APIs. Both have been published to NuGet. Note that the NuGet packages do require the proper SDKs to be installed, you can find download links here.

Right now Observable Kinect is a very thin wrapper around the existing APIs, though the speech package does help to simplify the speech SDK. Feel free to use Observable Kinect, expose issues, send pull requests, and just generally kick the tires.

Let me know what you think.

Categories: ObservableKinect Tags: , , ,

Rx – Remove from an ObservableCollection after a specified time

September 27th, 2010 1 comment

There was an interesting question on StackOverflow today that leads to another good example of the power of Reactive Extensions. The question was “How can I remove an item from an ObservableCollection some fixed amount of time after it was added?” With Rx it’s extremely simple! Here’s a quick console application demonstrating how to do it.

using System;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Linq;
using System.Threading;

namespace Test_App
{
    class Program
    {
        static void Main(string[] args)
        {
            var oc = new ObservableCollection<int>();

            var disposer =
             Observable
              .FromEvent<NotifyCollectionChangedEventArgs>(oc, "CollectionChanged")
              .Where(e => e.EventArgs.Action == NotifyCollectionChangedAction.Add)
              .Delay(TimeSpan.FromSeconds(5))
              .Subscribe(
                  e =>
                  {
                      foreach (int i in e.EventArgs.NewItems)
                      {
                          Console.WriteLine(string.Format("Removing item {0} at {1:HH:mm:ss}", i, DateTime.Now));
                          oc.Remove(i);
                      }
                  });

            for (int i = 1; i < 10; i++)
            {
                Console.WriteLine(string.Format("Added item {0} at {1:HH:mm:ss}", i, DateTime.Now));
                oc.Add(i);
                Thread.Sleep(1000);
            }

            Console.WriteLine("Press any key to exit..");
            Console.ReadKey(true);

            disposer.Dispose();
        }
    }
}

And here’s the output:

Added item 1 at 10:21:44
Added item 2 at 10:21:45
Added item 3 at 10:21:46
Added item 4 at 10:21:47
Added item 5 at 10:21:48
Added item 6 at 10:21:49
Removing item 1 at 10:21:49
Removing item 2 at 10:21:50
Added item 7 at 10:21:50
Added item 8 at 10:21:51
Removing item 3 at 10:21:51
Added item 9 at 10:21:52
Removing item 4 at 10:21:52
Press any key to exit..
Removing item 5 at 10:21:53
Removing item 6 at 10:21:54
Removing item 7 at 10:21:55
Removing item 8 at 10:21:56
Removing item 9 at 10:21:57

Pretty simple huh?

Categories: Code Samples Tags:

Rx – Calling a long running function asynchronously

August 25th, 2010 3 comments

Time for a little more Rx love. Today we’re going to call a long running function asynchronously and handle the return on the Dispatcher thread almost as simply as calling the function directly.

void CallSomeLongFunctionAsync()
{
 Observable
 .ToAsync<int>(LifeTheUniverseEverything)()
 .ObserveOnDispatcher()
 .Subscribe(theAnswer => MessageBox.Show(string.Format("The answer was {0}. What was the question anyway?", theAnswer)));
}

private int LifeTheUniverseEverything()
{
 Thread.Sleep(600000);
 return 42;
}

I may have used my new-found powers to calculate the answer to life, the universe, and everything but imagine a service call or long running file operation. Making those UIs completely non-blocking just got a lot easier.

Categories: Code Samples Tags:

Rx – Running a function when the user stops typing

August 18th, 2010 2 comments

I’ve been playing with Rx (Reactive Extensions) quite a bit lately and I have to say it’s one of the most powerful features added to .Net since LINQ. That probably isn’t too surprising since a lot of people call Rx “LINQ to Events”.

One common task when creating UIs is to call some function, like a filter or search, when the user stops typing. Anyone who has set this up before knows how painful it is but with Rx it’s dead simple.

private IDisposable textBoxObserver;

void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
    textBoxObserver =
        Observable
        .FromEvent<TextChangedEventArgs>(SomeTextBox, "TextChanged")
        .Throttle(TimeSpan.FromMilliseconds(500))
        .ObserveOnDispatcher()
        .Subscribe(HandleSomeTextBoxTextChanged);
}

void MainWindow_Unloaded(object sender, RoutedEventArgs e)
{
    textBoxObserver.Dispose();
}

private void HandleSomeTextBoxTextChanged(IEvent<TextChangedEventArgs> args)
{
    var tb = ((TextBox)args.Sender);
    var text = tb.Text;

    SomeFunctionYouWantToCallWhenTheUserStopsTyping(text);
}

There are a lot of other really cool uses for Rx so I’ll be posting more soon. Until then you should check out 101 Rx Samples or this simple implementation of drag & drop in Silverlight.

Categories: Code Samples Tags: